简体   繁体   English

从主屏幕重新启动应用程序后,如何阻止恢复某些活动?

[英]How do you stop a certain Activity from being resumed when you relaunch an app from the homescreen?

My Android app has a main activity, where a user clicks a button, which starts a "loading activity" with a spinner, which starts the results activity when finished loading. 我的Android应用程序有一个主要活动,即用户单击一个按钮,该按钮将使用微调器启动“加载活动”,并在完成加载后启动结果活动。

The loading activity is simply an onCreate function that spawns a new thread that fetches information from the web. 加载活动只是一个onCreate函数,它会产生一个新线程,该线程从Web上获取信息。

Often times, after using my app for a while, the loading activity will be the one that is shown when resuming my app from the homescreen. 通常,使用我的应用程序一段时间后,加载活动将是从主屏幕恢复我的应用程序时显示的活动。 The activity runs forever, spinner spinning the whole time. 该活动永远进行,旋转器一直旋转。 The only thing I can do is press the home key to close the app (and the problem remains until force closing the app). 我唯一能做的就是按Home键关闭该应用程序(问题一直存在,直到强制关闭该应用程序为止)。 How do you prevent this? 您如何防止这种情况? I want my main activity to start in these cases. 在这些情况下,我希望开始我的主要活动。

I've tried adding the following override function, but it didn't work: 我尝试添加以下覆盖功能,但没有成功:

@Override
public void onResume() {
    super.onResume();
    finish();
}

尝试将finish()放入您不想重新启动的活动的onPause()中。

Don't load data in a separate Activity. 不要在单独的活动中加载数据。 This is what threads are for. 这就是线程的用途。 Load your data in the background, and populate the UI in the relevant Activity when the data is returned. 在后台加载数据,并在返回数据时在相关活动中填充UI。

You could use AsyncTask for this, or Loaders if you don't want to mess with threads. 您可以为此使用AsyncTask ,或者如果您不想弄乱线程,可以使用Loaders

I control the lifecycle of my app by having a method called "determineAction()", that checks various logical states. 我通过使用一种名为“ determineAction()”的方法来控制我的应用程序的生命周期,该方法可以检查各种逻辑状态。 On the launch Activity, if it's valid, it continues loading, otherwise it redirects to other activities. 在启动活动上,如果有效,它将继续加载,否则将重定向到其他活动。 So no need to stop or pause the activity, just reroute it to the place you want it to be. 因此,无需停止或暂停活动,只需将其重新路由到您想要的位置即可。

Have you considered using LoaderManager and LoaderCallbacks<> to implement your asynchronous loading? 您是否考虑过使用LoaderManagerLoaderCallbacks<>来实现异步加载? They added this to Android starting in Honeycomb specifically for things like what you're trying to accomplish. 他们从Honeycomb开始将其添加到Android中,专门用于您要完成的事情。

http://developer.android.com/reference/android/app/LoaderManager.html http://developer.android.com/reference/android/app/LoaderManager.html

Without more specifics, it's hard to tell you the best way to approach this, but this may get you out of having a whole separate activity just to show a spinner while you load something. 没有更多的细节,很难告诉您解决此问题的最佳方法,但是这样做可能会使您无法进行单独的整个活动,而只是在加载内容时显示微调框。

Seems like a bad program structure and using finish like that will only make it worse. 好像程序结构不好,使用finish只会使情况变得更糟。 It seems your program hangs at certain times so you should focus on fixing that instead of hacking your way around. 看来您的程序在某些时候挂起了,所以您应该专注于解决问题,而不是乱搞。 Check if your callbacks is being called, check if you aren't creating more loading processes than you need, check if they are not interfering, etc. To prevent your program from loading stuff once it's been loaded, you could simply set a (static) variable and check that before starting the loading process. 检查是否调用了回调函数,检查是否创建的加载过程是否超过所需的加载过程,检查它们是否没有干扰等。为防止程序一旦加载就加载东西,可以简单地设置一个(static )变量,并在开始加载过程之前进行检查。

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

相关问题 如何防止 DialogFragment 在活动恢复时被解除 - How to prevent DialogFragment from being dismissed when activity gets resumed 如何避免使用主页按钮恢复活动? - How to avoid activity from being resumed using home button? 离开启动线程的活动后如何停止线程? - How do you stop a Thread when the Activity it was started in is left? Android从主屏幕还原应用程序时,切换到其他活动 - Android When restoring app from homescreen, switch to different activity 如何从内部视图中知道何时暂停/恢复活动? - How to know when activity is paused/resumed from inner view? 恢复时停止重新创建活动 - Stopping the activity from recreating when resumed 如何将Android App窗口小部件活动堆栈与应用程序活动堆栈分开? - How do you separate an Android App Widget Activity stack from the Application Activity stack? 如何在每次开机时阻止RTC_WAKEUP被呼叫? - How do you stop RTC_WAKEUP from being called everytime the phone boots? 您如何将数据从当前活动传递到上一个活动 - How do you pass data from current activity to Previous activity 你如何从另一个活动中调用主启动器活动? - How do you call the main launcher activity from another activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM