简体   繁体   English

处理活动状态的最佳方法

[英]Best way to handle activity state

I'm making an app that shows another activity when I correctly login however in the home screen when I pressed back and then returned to the app the login activity showed. 我正在制作一个应用程序,当我正确登录时该应用程序显示另一个活动,但是当我按下后又返回到登录活动显示的应用程序时,在主屏幕中显示了该活动。

I was able to avoid this overriding the onBackPressed this way: 我能够避免这种方式覆盖onBackPressed:

public void onBackPressed()
        moveTaskToBack(true);
    }

Is this the best way to do it? 这是最好的方法吗? Is there a more proper way to keep the state of the application when I exit it? 退出应用程序时,还有一种更合适的方法来保持应用程序的状态吗?

In your login activity after starting intent call finish() 在开始意图调用后的登录活动中finish()

finish destroy your activity and avoid to run it again automatically. 完成销毁您的活动,并避免自动再次运行。

Your issue is that you kept LoginActivity in stack. 您的问题是您将LoginActivity保留在堆栈中。 so when you press back it will kill MainActivity(after login) and since LoginAcitivity still there. 因此,当您按回时,它将杀死MainActivity(登录后),并且由于LoginAcitivity仍然存在。 it will be shown again. 它将再次显示。 best is to kill LoginAcitivty after starting new activity. 最好是在开始新活动后杀死LoginAcitivty。

call this in LoginActivity: 在LoginActivity中调用此命令:

Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
finish();

As you question I understand, you want to clear the entire history stack and start a new activity. 如您所知,您想清除整个历史记录堆栈并开始新的活动。 For this, in API level 11 a new Intent Flag was added which is: Intent.FLAG_ACTIVITY_CLEAR_TASK 为此,在API级别11中添加了一个新的Intent标志,即: Intent.FLAG_ACTIVITY_CLEAR_TASK

Just to clarify, use this: 只是为了澄清,使用此:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

For API <=10 now one can use IntentCompat class for the same. 对于API <= 10,现在可以使用IntentCompat类。 One can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK flag to clear task. 可以使用IntentCompat.FLAG_ACTIVITY_CLEAR_TASK标志清除任务。 So you can support pre API level 11 as well. 因此,您也可以支持API之前的11级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android上处理活动堆栈的最佳方法 - Best way to handle Activity Stack on Android 在 Android 中处理多个活动实例的最佳方法 - Best way to handle multiple instance of an activity in Android 在Android平台上保持活动(或应用)运行状态的最佳方法是什么? - What is the best way for keep running state of activity(or app) on android platform? 使用视图 model 作为 state 句柄来处理 state 的最佳方式是什么? - What would be the best way to handle state in compose avoiding recomposition using a view model as a state handle? 处理资源的最佳方法 - best way to handle resources 有没有办法处理在暂停状态下被杀死的 Android 应用程序,以使系统自动重新启动活动? - Is there a way to handle an Android app getting killed in the Paused state to make the system restart the activity automatically? 在滑行中处理IllegalArgumentException(“你无法为被破坏的活动开始加载”)的最佳方法是什么? - what is the best way to handle the IllegalArgumentException(“You cannot start a load for a destroyed activity”) in glide? Android:多个视图,深层导航,一个Activity。 什么是最好的处理方式? - Android: Multiple views, deep navigation, one Activity. What is the best way to handle? android - 处理大量图像和视图时处理保存片段状态的最佳方法 - android - best way to handle saving fragment state when dealing with lots of images and views 在这个活动中我应该如何处理 state? - How should I handle state in this Activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM