21

Scenario :

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

Case 1:

If I open my app by clicking icon again, the app restarts from the first activity.

Case 2:

If I open my app from recently open apps (in 4.0 by pressing menu button and selecting my app) it starts from the paused state.

I want the behavior 2 always to occur, don't want my app to restart every time when it is opened by clicking icon.

I have compared my manifest file with other apps and they are similar to mine, but behave differently (i.e like 2nd case which i want).

Edit:

This has been asked here : App completely restarting when launched by icon press in launcher

but no answers :(

Community
  • 1
  • 1
sanjeev
  • 1,343
  • 3
  • 16
  • 28
  • you share your first Activity code here, because from that we found what is the problem behind that. – Mr.Sandy Jul 09 '13 at 10:37
  • its a splash activity does not do anything except showing an image for 2 secs and then starting another activity – sanjeev Jul 09 '13 at 10:38
  • May be it has something to do with manifest file. can you post it? – Naresh Jul 09 '13 at 10:39
  • If you don't have any problem than post splash Activity code here... – Mr.Sandy Jul 09 '13 at 10:40
  • to @user3422577 your edit was rejected because it sounds too much like a comment you should post in the comments. I'll quote you: "*This is for first time when you install the application, if when you close the app in background and than work the behavior is same.*" But I don't even understand what you meant. Doesn't seem like you really read the question. – cregox Mar 15 '14 at 08:17

5 Answers5

25

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 :)

cregox
  • 17,674
  • 15
  • 85
  • 116
sanjeev
  • 1,343
  • 3
  • 16
  • 28
  • You should accept your own answer, if this was the solution indeed. – cregox Mar 15 '14 at 08:17
  • 3
    Hello, I'm having the same problem; but I have not any launchMode flag anywhere. Could you elaborate a bit more on how you resolved using the `onSaveInstanceState` function? (I suppose you meant onSaveInstanceState ) – ocramot Apr 24 '15 at 12:19
  • 2
    It was actually not required. Its working for me even after I have deleted the onsaveInstance code. – sanjeev Apr 27 '15 at 12:26
  • In my case, I have it for the entire application tag in the manifest – Harsha Jan 09 '20 at 12:57
  • Hi, the launchMode solved my problem. Recently, I have a webview. When I minimized the app and click the icon again, it somehow closed. This solved that. Thank you !! – Luki Centuri Nov 08 '21 at 09:04
6

Add this to your launcher activity:

if (!isTaskRoot()) {
    finish();
    return; 
}
super.onCreate(savedInstanceState);
Bolling
  • 3,954
  • 1
  • 27
  • 29
0

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.

Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
Desu
  • 464
  • 4
  • 6
0

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

Mohd Qasim
  • 896
  • 9
  • 20
-1

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();
}
}
Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54