In this situation, I will move on to one of two approaches, singleTask in manifest.xml OR in the Activity onResume() and onDestroy() methods, respectively.
For solution first : I prefer to use singleTask for activity in the manifest, rather than singleInstance , using singleInstance . I found out that in some cases creating activity is a new separate instance for myself, the result of which is the presence of two separate application windows in running applications in bcakground and in addition to additional memory allocations, which can lead to a very bad user experience when the user opens the application view, Choose an application to renew. Thus, the best way is for the specific activity in the manifest.xml file to be as follows:
<activity android:name=".MainActivity" android:launchMode="singleTask"</activity>
You can check activity trigger modes here .
To solve second, you just need to define a static variable or a preference variable, for example:
public class MainActivity extends Activity{ public static boolean isRunning = false; @Override public void onResume() { super.onResume();
and on the other hand, when you want to start this operation, just check:
private void launchMainActivity(){ if(MainActivity.isRunning) return; Intent intent = new Intent(ThisActivity.this, MainActivity.class); startActivity(intent); }
Muhammed Refaat Apr 09 '17 at 11:38 on 2017-04-09 11:38
source share