2

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.

2
  • why not just finish the detail activity in onResume() and not finishing list activity when going to details screen. Make a boolean variable true in onPause and in onResume check that variable before finishing Commented Mar 9, 2016 at 10:58
  • There were some flags you could use not to add the activity to the stack. Not sure if this will help but have a look here: stackoverflow.com/questions/12358485/…
    – vkislicins
    Commented Mar 9, 2016 at 11:01

5 Answers 5

1

Use SharedPreference to save the time of your paused detail activity in onPause and when it resume check the saved time with current time whether it has passed your threshold if it is passed then close it otherwise remain it opened.

1
  • Yes, that is what I say I would do if nothing better comes up. Commented Mar 9, 2016 at 16:14
0

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.

0

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);
    }
-1

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 a a 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.

2
  • I have several activities, not just a single one with fragments. Commented Mar 9, 2016 at 11:40
  • -1 ? Seriously ? Anyways :). You might adopt your solution to Activities even easier than the situation in which you've fragments as user5488874 suggested. Since you didn't specify anything about it, I assumed you might have used fragments.
    – stdout
    Commented Mar 10, 2016 at 7:41
-1

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"));
}
1
  • I don't see why that would help with the problem I have. Commented Mar 9, 2016 at 16:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.