开发者

How to clear an activity from the second activity going back to home screen in android?

开发者 https://www.devze.com 2022-12-21 15:28 出处:网络
example scenario is: from login screen - main screen - then when i clicked a hide button the app will go to home screen, and when im going to click开发者_StackOverflow the app again the main screen wo

example scenario is: from login screen - main screen - then when i clicked a hide button the app will go to home screen, and when im going to click开发者_StackOverflow the app again the main screen would be called


Fire an intent when you want to display the home screen

Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent); 

So this will be fired on the pressing of your hide button


I think you can use you can use FLAG_ACTIVITY_CLEAR_TOP

FirstActivity is the first activity in the application:
public static void home(Context ctx) {
    if (!(ctx instanceof FirstActivity)) {
        Intent intent = new Intent(ctx, FirstActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ctx.startActivity(intent);
    }
}
And If you want to exit from the whole application,this help you in that.
 public static void clearAndExit(Context ctx) {
    if (!(ctx instanceof FirstActivity)) {
        Intent intent = new Intent(ctx, FirstActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Bundle bundle = new Bundle();
        bundle.putBoolean("exit", true);
        intent.putExtras(bundle);
        ctx.startActivity(intent);
    } else {
        ((Activity) ctx).finish();
    }
}
i Really Hope this helps.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号