AgentKnopf
AgentKnopf

Reputation: 4335

Android: Get Intent despite calling finish() on the previous Activity

My use-case is as follows:

In activity A I call:

startActivity(B);
finish();

Now in onCreate of Activity B I need to know the activity that started B, so I wonder if in onCreate(...) of Activity B I call:

getIntent();

Would I even be able to get the Intent that started Activity B or would getIntent() at that point already return null because I finished Activity A immediately after calling startActivity(B) ?

Upvotes: 0

Views: 1950

Answers (2)

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

In first activity use below code to start new activity and restart method() to finish the activity::

Intent intent = new Intent(ThisActivity.this, NextActivity.class); 
intent.putExtra("Key", "Value");
startActivity(intent);

@Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        finish();
    }

In Second activity::

String started = getIntent().getStringExtras("Key");

Upvotes: 1

Mayank
Mayank

Reputation: 8852

here

Intent intent = new Intent(A.this, B.class); 
intent.putExtra("activityStarted", "A"); 

and in Activity B

String started = getIntent().getExtras().getString("activityStarted");

Upvotes: 5

Related Questions