1

How do I know if an Activity A was started from Intent:

Intent intent = new Intent(this, Activity.class);
startActivity(intent);

or due to activity lifecycle (after destroy, activity A can be created again, if it is on history applications).

Is there some way to distinguish these two ways to invoke an Activity?

Android Activity Lifecyce

richardaum
  • 6,651
  • 12
  • 48
  • 64

2 Answers2

2

use PutExtras() on the Intent Activity

check this or that or this-indirect-post

When your Activity is Recreated it is created with a different Intent not the Intent that was used to spark it initially-(especially from history), so if you use extras and check in your oncreate you will be better of -(read this with regards to the not soo much indirect post)

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • This approach using `putExtras(.)` is more applicable to this scenario, bcause `Intent` coming from `Activity` can not loose its `mExtras`. – richardaum Jun 28 '15 at 23:33
2

So if I understood you want to check if onCreate it's called a second time. You can achieve that by using this logic:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedIntanceState != null && savedIntanceState.getBooleanExtra("FIRST_RUN", false)){
        //not a first run
    }
}

protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean("FIRST_RUN", true);
    super.onSaveInstanceState(outState);
}
GuilhE
  • 11,591
  • 16
  • 75
  • 116