0

I'm unable to make my app to run only one instance. When I press on the app to start it then press the home button and press on the app start another instance the previous instance is not being killed before starting a new one. When the app is loaded I press back button to close it and then another instance pops up right after closing the first one...

AndroidManifest.xml

<activity android:name=".StartUpActivity" android:configChanges="orientation|screenSize" android:label="@string/app_name" android:noHistory="true" android:launchMode="singleTask"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

StartUpActivity

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loading); new Thread(new Runnable() { @Override public void run() { Intent activityIntent; // DO STUFF activityIntent = new Intent(context, RegNewUser.class); startActivity(activityIntent); } }).start(); } 
1
  • "Prevent android to start another instance of app" -- there will only ever be one instance of your app. There may be more than one instance of an activity. An activity is not an app. Commented Aug 12, 2016 at 16:37

1 Answer 1

3

Do not create a new thread to start a new activity. This code must be executed in the UI thread.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_loading); Intent activityIntent; activityIntent = new Intent(context, RegNewUser.class); startActivity(activityIntent); } 
Sign up to request clarification or add additional context in comments.

2 Comments

is it ok if i start a new activity inside OnPostExecute of AsyncTask?
it is clearly much better. However, I suggest to create a "communication channel" between your thread and the UI thread. You can for instance use an Handler developer.android.com/training/multiple-threads/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.