abh22ishek
abh22ishek

Reputation: 2671

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

Upvotes: 4

Views: 8339

Answers (4)

Virat18
Virat18

Reputation: 3505

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.

Upvotes: 11

Bob Snyder
Bob Snyder

Reputation: 38289

Although setting the launch mode to singleTask will work, use of that launch mode is discouraged. The documentation for launch mode indicates singleTask is "not recommended for general use".

The desired behavior can be achieved using Intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP:

    Intent intent=new Intent(ActivityC.this,ActivityA.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();

Activity A will not be recreated and will receive the intent via onNewIntent().

Upvotes: 2

Srikanth
Srikanth

Reputation: 1575

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.

Upvotes: 5

RaViMete
RaViMete

Reputation: 161

Use SingleTask launch mode for Activity A.
New intent will be delivered to existing instance.

Upvotes: 0

Related Questions