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

Go To StackoverFlow.com

0

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) ?

2012-04-04 06:38
by AgentKnopf
you can pass activity name by caliing intent.putExtra("AvtiVityName","Activity A" - Nishant 2012-04-04 06:41
Woudln't it be worth trying it ? lo - Snicolas 2012-04-04 06:44
@Nishant: Thanks I'll give that a sho - AgentKnopf 2012-04-04 06:46
but your Activity A finish after starting Activity B so getIntent(); always give value you have send from Activity A. try it - ρяσѕρєя K 2012-04-04 06:46
@Snicolas well thing is; I wanted to know a bit of background information about it :) plus: I am working on a massive framework - so if I want to try the above I'd have to change a lot, just to see if it could work. So I'd rather know before I try - AgentKnopf 2012-04-04 06:47
@imrankhan Thanks I thought so, but I wasnt sure - AgentKnopf 2012-04-04 06:47
possible duplicate of How to find Intent source in Android?Snicolas 2012-04-04 06:50
@Snicolas Basically true, except that the important part here was the call to finish() which wasn't quite clear to me in regard to the order of execution. I searched for the same use-case as mine but did not find a suitable question/answer, so I posted it - AgentKnopf 2012-04-04 07:00


5

here

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

and in Activity B

String started = getIntent().getExtras().getString("activityStarted");
2012-04-04 06:46
by Mayank
same as the first comment on the OP, but yeah easiest solution - Aadi Droid 2012-04-04 06:51
+1 & Accepted, Thank you - that does the job - AgentKnopf 2012-04-04 10:23


1

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");
2012-04-04 07:23
by Shankar Agarwal
Thanks for your answer - but what would be the purpose of calling finish() in onRestart? Isn't that rather dangerous - AgentKnopf 2012-04-04 10:22
did that coz as your you want to finish the current activity. so i done when you return from activity and it is not dangerou - Shankar Agarwal 2012-04-04 10:37
Thank you Agarwal Shankar, it's useful for my menu syste - Trung NT Nguyen 2015-05-31 17:18
Ads