简体   繁体   中英

Restart app from launch screen instead of resuming

I have an application with basically a list of items and a detail screen for each items.

When initially started, we show the list of items. If the user switches to another app when viewing a details screen, when he comes back to the app, the details screen is shown.

All that is the standard, and working well. However, my client needs the user to come back to the list screen instead of the details screen each time the app is resumed.

My first idea would be to remember the time at which the details activity got paused, and when started, if the time is greater than X seconds, finish and launch list activity instead of resuming.

Any more reliable way to do that?

PS: I know we should not do that, I already explained that to my client, decision is not mine.

使用 SharedPreference 将暂停的细节活动的时间保存在 onPause 中,并在它恢复时检查保存的时间与当前时间是否已超过您的阈值,如果超过则关闭它,否则保持打开状态。

Implement this solution and it definitely helps.

Basically, the app is not actually restarting completely, but your launch Activity is being started and added to the top of the Activity stack when the app is being resumed by the launcher. You can confirm this is the case by clicking the back button when you resume the app and are shown the launch Activity. You should then be brought to the Activity that you expected to be shown when you resumed the app.

The workaround I chose to implement to resolve this issue is to check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity. If those two flags are present and the Activity is not at the root of the task (meaning the app was already running), then I call finish() on the initial Activity. That exact solution may not work for you, but something similar should.

Here is what I do in onCreate() of the initial/launch Activity:

if (!isTaskRoot()
            && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
            && getIntent().getAction() != null
            && getIntent().getAction().equals(Intent.ACTION_MAIN)) {

        finish();
        return;
    }

for more details on isTaskRoot() method reference.

You have to provide the following onPause() method to all the activity classes except your list_item activity(Initial Activity).

@Override
    protected void onPause() {
        super.onPause();
        Intent i = new Intent(getApplicationContext(), list_item_activity.class);
        startActivity(i);
    }

I might understand your problem incorrectly but why you do all the timing stuff? I mean, assuming you've fragments for your list and detail views, just put aa flag to monitor your activity has stopped and listen to catch your activity resume ( via onResume or onWindowFocusChanged ). If it's stopped and resumed then transition to list fragment if it's not already visible.

You can you a broadcast receiver here. And on activity OnResume method use a call to broadcast receiver and perform whatever you need like this.

@Override
protected void onResume() {
    super.onResume();
        sendBroadcast(new Intent("YourActionHere"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM