Domanda

I have four activities, ABCD.

The user will initially go ABCD, then the user may want to tap the back button to check something on C before tapping a button on C to take them forward to D again.

The problem I have is that D is running a count down timer which needs to continue counting down and updating on an activity D view. If I currently tap back to go back to C then tap to go forward to D again, the countdown timer is still running but not connected as a new activity has been created for D.

I'd like the user to be able to navigate back to C without it affecting or destroying D so I can keep my timer hooked up to the view.

Finally, if the user clicks back to B, it would destroy both C and D activities (and the countdown timer).

È stato utile?

Soluzione

It's quit easy save the timer value and current time on saveInstanceState call back when users leave the activity. When activity recreates use the timer value plus it with (current time - lastSavedTime) and set the timer.


it should work just fine but you can achieve this in better way by using fragments and ViewPager.

In case you didn't used ViewPager before. its a component that let you switch between different views by swiping screen to left or right. By default view pager loads the current view and one near it in both side. so if you are in page 1 (index 0) page 1 and page 2 is loaded. if your are in page 2, page 2, page 1, and page 3 will be loaded and so on. The good news is you can change the number of pages that will load near each fragment using simple code like this

mViewPager.setOffscreenPageLimit(3);

So now all 4 fragments are loaded. So you have 4 fragments all loaded on screen. According to developers.android.com all the others views (except one on screen) are loaded but in idle state so I don't think you can have your timer inside fragment D. But that wouldn't be a problem because you only have one activity. Implement your timer in the one and only activity you've got and it will work fine because this activity will not be destroyed when pages change. But you still need to save the same data (timer data + current time) because activity may stop due to user switching to another app.

last thing you need to do is accessing timer data inside your fragment. I will not explain how since there is so many tutorial on how to communicate between activities and fragments but just to give you and idea you need to declare an interface inside your fragment, implement this interface in activity and use this interface inside your fragment to access timer data.

The second solution is a lot more complex but it gives the user a better experience and performance increase is noticeable. Decide witch one is best for you. Good luck

Altri suggerimenti

You can use this

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, C.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
}

to put your D activity to background and then resume it from C like this

Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

However, you should use a service for this sort of background tasks.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top