4

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(); 
abh22ishek
  • 2,631
  • 4
  • 27
  • 47
  • I don't know how to avoid called `onCreate` **but** you can put a **flag** (and once it true skip onCreate method using return;) – AsfK Dec 10 '15 at 07:19
  • You could add an `extra` to your `intent`, which you retrieve in the `onCreate` and write a condition about the code, which should not be executed in this case. – yennsarah Dec 10 '15 at 07:19
  • android fragment is the best option for this – Sree Dec 10 '15 at 07:19
  • While moving to C from B, finish B. Now when you will finish C you will come back to A by default and onCreate wont be called. – Rohit5k2 Dec 10 '15 at 07:23
  • @Rohit5k2 i have a condition here , i can't go to B .(In real case scenario i have more 10 activity) so its not possible to put finish statement on every one of them – abh22ishek Dec 10 '15 at 07:27
  • @AsfK what flag can you elaborate a little – abh22ishek Dec 10 '15 at 07:28
  • You can return to your activity (A) with data (pass data between activities), put a boolean flag... see this for more info: http://stackoverflow.com/questions/1124548/how-to-pass-the-values-from-one-activity-to-previous-activity – AsfK Dec 10 '15 at 07:31
  • Some explanation on WHY it happens: (From Android Docs) Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard". Link: https://developer.android.com/guide/components/activities/tasks-and-back-stack.html – 最白目 Jan 27 '17 at 09:24

4 Answers4

11

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.

Virat18
  • 3,427
  • 2
  • 23
  • 29
4

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.

Srikanth
  • 1,555
  • 12
  • 20
2

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

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I can confirm with `Intent.FLAG_ACTIVITY_CLEAR_TOP`, onCreate is call, with `Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP`, onNewIntent is called. I don't know why it does this way, not mentioned in docs. I thought with `FLAG_ACTIVITY_CLEAR_TOP` should trigger onNewnIntent. – Arst May 13 '16 at 14:25
0

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

RaViMete
  • 161
  • 3