public class MainActivity extends ActionBarActivity {
Button b1;
TextView tv2;
Integer count ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
b1= (Button)findViewById(R.id.b1);
tv2=(TextView)findViewById(R.id.tv2);
tv2.setText(count);
if(count == 5) {
Intent ii = new Intent(this,Activity2.class );
Bundle bb = new Bundle();
bb.putInt("Count",count);
ii.putExtras(bb);
startActivity(ii);
finish();
}
else
{
Intent iii = new Intent(this,activity3.class);
// Bundle bb1 = new Bundle();
startActivity(iii);
}
}
public void onStart()
{
Bundle b33 = getIntent().getExtras();
count=b33.getInt("count");
tv2.setText(count);
}
in this code i want to count the number of times an activity is opened in this case activity1 will open only 5 times and after that activity3 will open what i m trying is killing the activity1 before killing i m sending the count value to the activity2 and after that i m calling again activity1 and again i m sending the count value to activity1 so the code will be executed from the start itself so again the value will be 1 but i want activity1 to catch the new values of count in oncreate() or in onstart()
and the error is the app is not opening its force closing and in logcat Null-exception error is displayed although i did all the bindings.
try this code at onStart() method
public void onStart()
{
Bundle b33 = getIntent().getExtras();
if(b33 != null)
{
count=b33.getInt("count");
tv2.setText(count);
}
}
change:
Integer count;
to
int count=0;
also change:
tv2.setText(count);
to(in both place onCreate and onStart):
tv2.setText(String.valueOf(count));
Updated code:
public class MainActivity extends ActionBarActivity {
Button b1;
TextView tv2;
int count = 0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
b1= (Button)findViewById(R.id.b1);
tv2=(TextView)findViewById(R.id.tv2);
tv2.setText(String.ValueOf(count));
if(count == 5) {
Intent ii = new Intent(this,Activity2.class );
Bundle bb = new Bundle();
bb.putInt("Count",count);
ii.putExtras(bb);
startActivity(ii);
finish();
}
else
{
Intent iii = new Intent(this,activity3.class);
// Bundle bb1 = new Bundle();
startActivity(iii);
}
}
public void onStart()
{
super.onStart();
Bundle b33 = getIntent().getExtras();
if(b33 != null)
{
count=b33.getInt("count");
tv2.setText(String.valueOf(count));
}
}
i tried this code and its working.
Related
My app has two button, first button will show a new Activity on second screen, and second button will update that Activity with some data.
But once the second Activity started, I cannot startActivity again. Also, I cannot kill the second Activity. How can I kill the Activity and then restart it?
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.btnShowScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showSecondDisplay();
}
});
binding.btnSetData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showSecondDisplayWithData();
}
});
}
private void showSecondDisplay(){
DisplayManager displayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
if(displays.length > 1){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(displays[1].getDisplayId());
startActivity(
new Intent(this, MainActivity2.class),
options.toBundle()
);
}
}else{
Toast.makeText(this,"No second display", Toast.LENGTH_SHORT).show();
}
}
private void showSecondDisplayWithData(){
DisplayManager displayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
if(displays.length > 1){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchDisplayId(displays[1].getDisplayId());
Intent intent = new Intent(this, MainActivity2.class);
intent.putExtra("data", "this is data");
startActivity(intent, options.toBundle());
}
}else{
Toast.makeText(this,"No second display", Toast.LENGTH_SHORT).show();
}
}
}
public class MainActivity2 extends AppCompatActivity {
private ActivityMain2Binding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMain2Binding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Bundle extras = getIntent().getExtras();
if (extras != null) {
String data = extras.getString("data");
binding.viewData.setText(data);
}
}
}
I want to make a button in an activity disabled if it is intented from a certain button and otherwise enabled if and it is intented from some other class.What I am saying will be clear after reading the code.
This is the function to intent the class containing the button.
showperson.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
falses=true;
ob.truth=false;
Toast.makeText(home_page.this, "doneit"+ob.truth, Toast.LENGTH_SHORT).show();
// Button clik=fb.getButton();
// clik.setVisibility(View.GONE);
Intent to=new Intent(home_page.this,Form.class);
to.putExtra("buttonclik",false);
startActivity(to);
}
});
Try to add this in onCreate():
Intent intent = getIntent();
if (intent != null) {
boolean isButtonClicked = intent.getBooleanExtra("buttonclik", false);
if (isButtonClicked) {
showperson.setEnabled(false);
}
}
And change false to true at to.putExtra("buttonclik",false);
You can achieve that by setting a flag from the caller activity. Refer to the example below.
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Intent intent = new Intent(this, ActivityThatContainsTheButton.class);
intent.putExtra("activity", "activityA");
startActivity(intent);
}
}
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = new Intent(this, ActivityThatContainsTheButton.class);
intent.putExtra("activity", "activityB");
startActivity(intent);
}
}
public class ActivityThatContainsTheButton extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x);
Button button = findViewById(R.id.button);
String from = getIntent().getStringExtra("activity");
if (from != null) {
if (from.equals("activityA")) {
button.setVisibility(View.VISIBLE);
} else if (from.equals("activityB")) {
button.setVisibility(View.INVISIBLE);
}
}
}
}
I wish that when I click on the apply button firstapp, opens secondapp thus the data passed the press: What did I do wrong?
Secondapp is called but it print null.
App1
package pkg.firstapp;
public class MainActivity extends Activity{
private static final int REQUEST_CODE_START_APP = 12345;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PackageManager manager = getApplicationContext().getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage("pkg.secondapp");
if (i != null)
{
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.putExtra("key1", "value1");
i.setFlags(0);
setResult(RESULT_OK, i);
startActivityForResult(i, REQUEST_CODE_START_APP);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
If you want to get String from Intent, you should call getIntent().getStringExtra("key1"); in onCreate. onActivityResult is called in first Activity after finishing second. Read this for more info
onActivityResult is called in the activity which started other activity for result.
In order to get the data you're sending, in the second activity's onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String value1 = getIntent().getStringExtra("key1");
}
I am making a countdown timer in android eclipse and I want to use a Textview from another class.
This is my first class
public class Main extends Activity {
Spinner timerValueSpinner;
Button startButton;
TextView statusTextView;
Countdown timer;
String[] timeValues;
Resources resourcePointer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner);
statusTextView = (TextView)this.findViewById(R.id.timerView);
resourcePointer = getResources();
timeValues = resourcePointer.getStringArray(R.array.seconds_list);
startButton = (Button)this.findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main.this,Timer.class);
startActivity(intent);
if(timerValueSpinner.getSelectedItemPosition() > -1){
int parsedSpinnerValue = 0;
parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]);
if(parsedSpinnerValue > 0){
if(timer != null){
timer.cancel();
}
timer = new Countdown(parsedSpinnerValue
* Countdown.oneSecond, Countdown.oneSecond,statusTextView);
timer.start();
}
}
}
});
}
This is my Second class and I want to get its statusTextVie and use it in the First(Main) class instead of its own textview so when I click the button the second class will show and the countdown will start from its textview.
public class Timer extends Activity {
TextView statusTextVie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
statusTextVie = (TextView)this.findViewById(R.id.timerVie);
}
This is my first time asking a question here, so I am sorry if i made some mistakes.
Why not pass the time value from the first activity to second activity via intent and start the thread in second activity ?
First Activity:
public class Main extends Activity {
Spinner timerValueSpinner;
Button startButton;
String[] timeValues;
Resources resourcePointer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner);
resourcePointer = getResources();
timeValues = resourcePointer.getStringArray(R.array.seconds_list);
startButton = (Button)this.findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main.this,Timer.class);
if(timerValueSpinner.getSelectedItemPosition() > -1){
int parsedSpinnerValue = 0;
parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]);
if(parsedSpinnerValue > 0){
intent.putExtra("TimeValue", parsedSpinnerValue);
startActivity(intent);
}
}
}
});
}
Second Activity:
public class Timer extends Activity {
Countdown timer;
TextView statusTextVie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
Bundle extras = getIntent().getExtras();
int timeVal = extras.getInt("TimeValue", 0);
statusTextVie = (TextView)this.findViewById(R.id.timerVie);
if (timeVal > 0){
if(timer != null){
timer.cancel();
}
timer = new Countdown(timeVal * Countdown.oneSecond, Countdown.oneSecond,statusTextVie);
timer.start();
}
}
}
i have following problem:
I've made a simple android app that adds 1 to an integer every 1000 ms using a handler, and then display this integer.
The problem is that when i start another activity the same thing happens, which would be fine, if that was intended. The mentioned function is not called in the new activity and yet it seems to be. Please look over my code and show me where it went wrong..
MainActivity:
public class MainActivity extends ActionBarActivity {
protected TextView text;
protected int position;
private Handler handler = new Handler();
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
position=0;
SetButtonCLickListener();
counter();
}
protected void SetButtonCLickListener() {
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SwitchActivity();
}
});
}
private void counter() {
handler.removeCallbacks(count);
handler.postDelayed(count, 1000);
}
private Runnable count = new Runnable() {
public void run() {
i++;
text.setText("Count: " + i);
handler.postDelayed(count, 1000);
}
};
protected void SwitchActivity() {
if (position == 1) {
finish();
} else {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
}
SecondActivity
public class MainActivity2 extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
text = (TextView) findViewById(R.id.text);
SetButtonCLickListener();
position=1;
}
}
MainActivity2 has an onCreate() method. In the onCreate() method, you call super.onCreate(), which triggers the MainActivity implementation of onCreate(). The MainActivity implementation of onCreate() is where you are starting your counter thing, via its call to the counter() method. Hence, when MainActivity2 starts up, its onCreate() calls MainActivity's onCreate(), which calls counter().
My guess is that MainActivity2 should inherit from Activity, not from MainActivity.