0

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Carlos Hernández Gil
  • 1,853
  • 2
  • 22
  • 30

6 Answers6

2

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.

Aster
  • 809
  • 2
  • 8
  • 13
2

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

    }
}
techroid
  • 477
  • 4
  • 11
1

Use a static variable to store the last activity started, so when activity A is resumed, you can check it.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20
  • Thank you for your answer, i will try this solutions, it seems to be the easiest to implement. I have a doubt thought, why must be static if i check its value in onStart or onResume? – Carlos Hernández Gil Aug 25 '15 at 10:13
  • Because you are going to share it throughout the application, otherwise you have to store the main activity reference at each activity, so they can write to the same variable. – Juanjo Vega Aug 25 '15 at 11:27
1
private boolean isB = false;

public void startActivity(Class<?> activityName){
    Intent intent = new Intent(this, activityName);
    isB = activityName.getName().equals(B.class.getName());
    startActivity(intent);
}
Filnik
  • 352
  • 1
  • 12
  • 33
  • Thank you also, i implemented my static variable as a boolean also. – Carlos Hernández Gil Aug 25 '15 at 10:25
  • not tried, but I think you shouldn't need a static variable, since the main activity should not be destroyed... if you want to add it to be safe, do it, but I think you won't need it – Filnik Aug 25 '15 at 10:34
1

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

Community
  • 1
  • 1
P-RAD
  • 1,293
  • 2
  • 15
  • 36
  • i have put the checking of the boolean variable in onStart method, although perhaps should i use instead onResume. I don´t know the benefits of using one over the other in this case – Carlos Hernández Gil Aug 25 '15 at 10:23
  • you should be using onResume() and not onStart() , When an activity pauses(another activity launches in front) onPause() getting called, when it comes back to screen onResume() is getting called and not onStart() , there are a lot of sites and youtube videos explaining this stuff.... – P-RAD Aug 25 '15 at 10:26
  • you are right, but onPause is followed almost always by onStop, i haven´t so far a case that don´t do that. I will follow your recommendations though. – Carlos Hernández Gil Aug 25 '15 at 10:32
  • if the activity is stopped completely try using onRestart() – P-RAD Aug 25 '15 at 10:35
0

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");
    }


}
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • Thanks for your answer ,i know more or less the lifecycle, but what i don´t understand is why almost always people use onResume or onStart instead onRestart – Carlos Hernández Gil Aug 25 '15 at 10:26