简体   繁体   中英

Change android activity while app is in background?

My android app detects if the app is in the background (say the user pressed the home menu) using ComponentCallbacks2 . Now, I want the app to change an activity while it is currently in the background without changing the users interface.

My only question is: How can I change the activity for when the user in the background, so that it pops up when he/she gets back to the app, rather than popping up as soon as I call startActivity(Intent i) .

Here are the steps that I want my app to execute:

  1. User opens home menu in app (WORKS)
  2. App detects that app is in background (WORKS)
  3. Using intents, open up another activity without disrupting whatever app the user is on (DOESN'T WORK)

Right now, steps one and two are going fine. But, when I use an intent to change the activity, the activity pops open...disturbing the user. Basically, I want to load the activity, so that as soon as the user opens the app back up, it goes to the game over screen.


My research on the topic, and what I've tried to resolve the issue on my own:

I have done lots of research on this topic, but the other questions are about running processes in the background. For this, obviously, one would use a service. My question is laser focused on how I can change the activity for when the user in the background, so that it pops up when he/she gets back to the app. I have also tried to use the onContinue() method, but that gets called every time the user changes the activity, making it unreliable.

Thanks for the expert advice:

Rich

My suggestion is to save your preferences in onStop (or when app is in background mode). Then use that info in OnCreate of SplashActivity to start Game-Over activity.

##

In your Activity where you detect background

  @Override protected void onStop(){ super.onStop(); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("isGameOver", isGameOver); // Commit the edits! editor.commit(); } 
######### Splash Activity
 public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); Thread timerThread = new Thread(){ public void run(){ try{ sleep(2000); }catch(InterruptedException e){ e.printStackTrace(); }finally{ SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean isGameOver = settings.getBoolean("isGameOver", false); if(isGameOver) { Intent intent = new Intent(SplashActivity.this,GameOverActivity.class); startActivity(intent); } else { // Start Main Activity } } } }; timerThread.start(); } @Override protected void onPause() { super.onPause(); finish(); } } 

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