richardaum
richardaum

Reputation: 6777

Activity started from background or for another activity

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

Upvotes: 1

Views: 124

Answers (2)

GuilhE
GuilhE

Reputation: 11861

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

Upvotes: 2

Elltz
Elltz

Reputation: 10859

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)

Upvotes: 2

Related Questions