Why does FLAG_ACTIVITY_CLEAR_TOP not work?
From the documentation for FLAG_ACTIVITY_CLEAR_TOP:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
As you added in your comment, the activity A has been finished before calling B, so this situation doesn't apply. A new instance of activity A will be launched instead.
As I see it, you have two options here:
1) Use the Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
flags. This will start activity A as the root of the stack. It works, but any other activities in the stack will be lost. Assuming A was the first activity (or at least, that you are not interested in any previous activities in the task stack) then it doesn't matter. Note: CLEAR_TASK requires API Level 11.
2) Another possible solution (in case the previous assumption is not true) would be to not use intent flags at all:
- B starts C with
startActivityForResult()
. - Instead of calling A, C finishes, having set a result for B indicating that A must be launched.
- In
B.afterActivityResult()
, finish B and launch A.
Intent.FLAG_ACTIVITY_CLEAR_TOP doesn't deletes the activity stack
Use this--
Intent intent = new Intent(Activity3.this, FinishActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
Edited--New Answer and would work perfectly..
just taking an example...do it accordingly what your project needs--
I am taking three Activity
class A, B and C..and I have applied a close button on the view of class C Activity. If you want then by the Back button you can go to the previous Activity and when you press the close button then you would exit from apps..have a look--
public class AActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, B.class);
startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
finish();
}
}
}
Take next class activity--
public class B extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
Intent i = new Intent(this, C.class);
startActivityForResult(i, 2);
}
@Override
protected void onActivityResult(final int requestCode,
final int resultCode, final Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == 5) {
setResult(5);
finish();
}
}
}
Take last activity--
public class C extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.c);
}
// close button..set by xml view..you can set it by button listener.
public void close(View v) {
setResult(5);
finish();
}
}
Hopefully, it would solve your problem..cheers!
Intent.FLAG_ACTIVITY_CLEAR_TOP is not working
Set these following flags :
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK
Android, Intent.FLAG_ACTIVITY_CLEAR_TOP seems doesn't work?
Try using this intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
inplace of intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent(getApplicationContext(),
yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP does not work as expected
The problem is in android:launchMode="singleInstance"
. According to the documentation for singleInstance:
If this activity tries to start a new activity, that new activity will
be launched in a separate task.
So, your activity D is launched as the new task, and that's why it is not deleted.
Android FLAG_ACTIVITY_CLEAR_TOP not working properly?
You can use the following methods to start and close child activity
public ArrayList<String> mIdList;
/**
* Starts an Activity as a child Activity to this.
*
* @param Id
* Unique identifier of the activity to be started.
* @param intent
* The Intent describing the activity to be started.
* @throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {@link LocalActivityManager#destroyActivity} on
* the child activity and starts the previous activity. If the last child
* activity just called finish(),this activity (the parent), calls finish to
* finish the entire group.
*/
@Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size() - 1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index);
index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
Example:
startChildActivity("Merchants", new Intent(this,MerchantScreen.class));
Related Topics
Startactivityforresult from Activitygroup
Intent Filter to Download Attachment from Gmail Apps on Android
Running Google Map Application on Android Emulator
Emulator: Warning: Quick Boot/Snapshots Not Supported on This Machine
Import .R Cannot Be Resolved When I Import Actionbarsherlock
Web App on Android Browser Width Issue
Vectordrawablecompat Resources$Notfoundexception on Kitkat and Below
Getting the Pixel Color Value of a Point on an Android View That Includes a Bitmap-Backed Canvas
Are There Conventions on How to Name Resources
Finish an Activity After a Time Period
How to Detect If Device Support Notch Display
Android Studio - Processdebugresources Failed
Widgets Don't Respond When Re-Added Through Code
Fcm Remote Notifications Payload for iOS and Android
Using Videoview for Streaming or Progressive-Download Video
What Are The Default Color Values for The Holo Theme on Android 4.0