Android: when you resume the application after clicking at home, the application always starts with root privileges

My use case for the game looks like this: I have a LoadActivity that loads (according to AsyncTask, but that doesn't matter) and saves all the graphics needed to play in a static class. When the download is complete, MenuActivity appears. From this action, I can run other actions, such as LikeBeginActivity.

This is a snippet of the manifest:

<activity android:name="my.domain.mygame.LoadingActivity" android:alwaysRetainTaskState="true" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="my.domain.mygame.MenuActivity" android:theme="@android:style/Theme.NoTitleBar" > </activity> <activity android:name="my.domain.mygame.LevelBeginActivity" android:theme="@android:style/Theme.NoTitleBar" > </activity> 

The calls are as follows:

LoadingActivity

 public void startMenu() { final Intent gameIntent = new Intent(this, MenuActivity.class); startActivity(gameIntent); } 

MenuActivity

 buttonStartNewGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent levelIntent = new Intent(MenuActivity.this, LevelBeginActivity.class); levelIntent.putExtra(IntentConstants.STARTLEVELINDEX, 0); startActivity(levelIntent); } }); 

The annoying problem is that when I am in LevelbeginActivity and I press HOME and resume the game with the launcher, it always starts with LoadActivity, but why is this whole task cleared? That doesn't make sense to me. Then I thought about adding

 android:alwaysRetainTaskState="true" 

for LoadingActivity in the manifest, but that doesn't help either. Why does he run the whole task again? This is very problematic because I am running out of memory if LoadingActivity starts up again and again. (The next question will be the behavior of the back button, but this is another problem). I could spoil some ugly loading behavior by storing the last Activity name as SharedPreferences or something like that, but I would prefer a clean solution for this.

+2
source share
2 answers

Android will destroy Activities if the system needs memory. You must ensure that you save the status of your activity in onSaveInstanceState() so that you can resume it later. You can save the required values โ€‹โ€‹in SharedPreferences or in SQLiteDatabase

0
source

Another instance of the application is running. This is the cause of the problem.

  // To prevent launching another instance of app on clicking app icon if (!isTaskRoot() && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) && getIntent().getAction() != null && getIntent().getAction().equals(Intent.ACTION_MAIN)) { finish(); return; } 

write the above code in your launch activity before calling setContentView. This will solve the problem.

0
source

Source: https://habr.com/ru/post/1202619/


All Articles