Reputation: 1853
I have an Activity A that uses an intent to go to Activity B. Sometimes Activity A start also another activity, Activity C.
Is there a way for Activity A to know from which Activity (B or C) it is restarted when these activities finish?
Upvotes: 0
Views: 94
Reputation: 477
From your Activity A, call following methods, suppose you have two buttons and on click of them start Activity B or Activity C:
public void StartOtherActivityB() {
Intent aIntent = new Intent(LauncherActivity.this, OtherActivityB.class);
startActivityForResult(aIntent, REQUEST_ACTIVITY_B);
}
public void StartOtherActivityC() {
Intent aIntent = new Intent(LauncherActivity.this, OtherActivityC.class);
startActivityForResult(aIntent, REQUEST_ACTIVITY_C);
}
and implement onActivityResult() method as follows
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ACTIVITY_B){
//class restarted from ACTIVITY_B
}else if (requestCode ==REQUEST_ACTIVITY_C){
//class restarted from ACTIVITY_C
}
}
Upvotes: 2
Reputation: 22955
check out your self , you will come to know easily
public class MainActivity extends Activity {
Button clikBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("onCreate", "111111111");
Toast.makeText(MainActivity.this,"onCreate...",Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
clikBtn = (Button)findViewById(R.id.clickBtn);
/*clikBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
}
});*/
}
public void clickButton(View v){
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(MainActivity.this,"onStart...",Toast.LENGTH_LONG).show();
Log.e("onStart","999999999");
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(MainActivity.this,"onPause...",Toast.LENGTH_LONG).show();
Log.e("onPause", "222222222");
// notify("onPause");
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(MainActivity.this,"onResume...",Toast.LENGTH_LONG).show();
Log.e("onResume", "333333333");
//notify("onResume");
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(MainActivity.this,"onStop...",Toast.LENGTH_LONG).show();
Log.e("onStop", "444444444");
//notify("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(MainActivity.this,"onDestroy...",Toast.LENGTH_LONG).show();
Log.e("onDestroy", "55555555");
//notify("onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(MainActivity.this,"onRestart...",Toast.LENGTH_LONG).show();
Log.e("onRestart","66666666");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(MainActivity.this,"onRestoreInstanceState...",Toast.LENGTH_LONG).show();
Log.e("onRestoreInstanceState", "777777777");
//notify("onRestoreInstanceState");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Toast.makeText(MainActivity.this,"onSaveInstanceState...",Toast.LENGTH_LONG).show();
Log.e("onSaveInstanceState", "8888888888");
//notify("onSaveInstanceState");
}
}
Upvotes: 0
Reputation: 1341
like Juanjo Vega said, use the activitylifecycle methods to do this..... take a look at the methods.... LifecycleMethods-android
you can set up a global variable or something (as per your logic) in the onResume() method of the activity which you want to get notified. onResume() will cal after onPause which trigger your logic...
for setting up globals you can use singleTons or the Application class
Upvotes: 1
Reputation: 138
private boolean isB = false;
public void startActivity(Class<?> activityName){
Intent intent = new Intent(this, activityName);
isB = activityName.getName().equals(B.class.getName());
startActivity(intent);
}
Upvotes: 1
Reputation: 877
You could use method startActivityForResult instead of startActivity to start your activity B or C. If B or C than returns a result that identifies itself you can read it.
Upvotes: 2
Reputation: 1440
Use a static variable to store the last activity started, so when activity A is resumed, you can check it.
Upvotes: 1