How to send my activity to background and resume?

2019-02-18 05:05发布

问题:

I want to load data from Internet.

While loading data I show a loading indicator. What I want to do when pressing back, I cancel the loading indicator via onCancelEvent(). I want to my activity to continue in background, so that the last activity is shown to front.

I don't want to finish() and start the last activity as all the data is shown in that, which will be loaded again if recreated.

Can any one help?

回答1:

Basically you want to switch the order of the current background and the previous background. To do this, you can start the previous activity and use the FLAG_ACTIVITY_REORDER_TO_FRONT flag:

Intent intent = new Intent(this, MyPreviousActivity.class);
intent.setFlags(FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

So if you have tasks A, B, C and task C calls startActivity() with activity B, then B will be brought to the front of the activity stack, with the resulting order A, C, B, which is what you were asking for.

See Intent.FLAG_ACTIVITY_REORDER_TO_FRONT



回答2:

Simply use:

moveTaskToBack(true);