Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to come back to First activity without its onCreate() called

I have 3 activity . Activity A ,Activity B, Activity C. This is the flow A->B->C. Now i want to come to activity A from C .(i.e C->A) without getting its onCreate() called of Activity A.

so far my code is:But it will call onCreate() of ActivityA. I want to call Restart().

Intent intent=new Intent(ActivityC.this,ActivityA.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish(); 
like image 270
abh22ishek Avatar asked Dec 06 '22 19:12

abh22ishek


2 Answers

Two cases:

  1. If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.

  2. If you don't want to clear all activities and want existing instance of A at top, then use intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.

Note : If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be called.

like image 81
Virat18 Avatar answered Dec 08 '22 07:12

Virat18


Keep android:launchMode="singleTask" in manifest at activity declaretion

An Activity with singleTask launchMode is allowed to have only one instance in the system If instance already exists, i will not call 'onCreate' it will call 'onNewIntent' method. See http://androidsrc.net/android-activity-launch-mode-example/ for better understand about launch modes.

like image 37
Srikanth Avatar answered Dec 08 '22 09:12

Srikanth