Android app restarts when opened by clicking app icon

后端 未结 5 524
情书的邮戳
情书的邮戳 2021-01-04 00:00

Scenario :

I open my app by clicking icon, do something, navigate through activities, pause the app by clicking home button.

Case 1:

If I

相关标签:
5条回答
  • 2021-01-04 00:30

    I found it. I had set a flag android:launchMode="singleTask" in my activity flag. I deleted that code.

    I also added onsaveInstance method to all the activities in my code and it's working now!

    Thanks :)

    0 讨论(0)
  • 2021-01-04 00:31

    Try to replace you splash Activity code with this code..

    public class Splash extends Activity {
    
    protected boolean _active = true;
    protected int _splashTime = 2000;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash);
    
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while (_active && (waited < _splashTime)) {
                        sleep(100);
                        if (_active) {
                            waited += 100;
                        }
                    }
                } catch (InterruptedException e) {
                    e.toString();
                } finally {
                    Intent intent = new Intent(getApplicationContext(),
                            MainActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        };
    
        splashTread.start();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    }
    
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
    //      super.onBackPressed();
    }
    }
    
    0 讨论(0)
  • 2021-01-04 00:40

    Add this to your launcher activity:

    if (!isTaskRoot()) {
        finish();
        return; 
    }
    super.onCreate(savedInstanceState);
    
    0 讨论(0)
  • 2021-01-04 00:45

    seems in AndroidManifest you kept your launch activity android:launchMode="singleTask". remove this one from your launch activity will solve problem

    0 讨论(0)
  • 2021-01-04 00:48

    In current activity set some image which needs to be displayed for 2 seconds like below.

    ImageView im = new ImageView(this);
    im.setImageResource(set your image);
    setContentView(im);
    intentMainScreen = new Intent(getApplicationContext(), MainScreen.class);
    Handler x = new Handler();
    x.postDelayed(new splashhandler(), 2000);
    

    Then start your activity in the SplashHandler class (which implements runnable and call start activity in run method).

    It will display your Splash screen for 2 seconds and start another activity.

    0 讨论(0)
提交回复
热议问题