-1

Basically I want to do an if statement like below, but I don't know how.

if (Android_Classes_Left_Open){
    Intent a = new Intent(SplashScreen.this, 2ndtoTopElement.class);
    startActivity(a);
} else {
    Intent a = new Intent(SplashScreen.this, Other.class);
    startActivity(a);
}

My SplashScreen code so far:

public class SplashScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(SplashScreen.this, First.class);
                startActivity(i);

                finish();
            }
        }, 3000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.splash, menu);
        return true;
    }
}

The purpose of this is, if the user moves the application into the background they are then returned to their most recently opened activity rather than starting the application from scratch each time. How can I achieve this?

  • 1
    What are "opened classes"? – Marcin Orlowski Nov 12 '14 at 09:55
  • 2
    Erm, isn't this the default behavior in Android?? – Fildor Nov 12 '14 at 09:57
  • When you use an android application, and click a given button to open up another activity. It then leaves that previous activity in the background (opened activity) –  Nov 12 '14 at 09:57
  • I have a splash screen which seems to open every time the Android application is opened. So what I'm trying to accomplish is, have the splash screen check what was the most recently used activities (left in background), if any. Then direct the user to that rather than another –  Nov 12 '14 at 09:59
  • You don't "open classes", you start Activities. Maybe this will clear things up: [Backstack](http://developer.android.com/guide/components/tasks-and-back-stack.html) – Fildor Nov 12 '14 at 10:00
  • My apologies, I've changed that throughout the thread now. Do you have any ideas how to accomplish the above? –  Nov 12 '14 at 10:03
  • Ah, your comment cleared it up a bit ... Can you show the code where you display the splash right now? I suspect, maybe it is enough to do it in a different eventHandler. – Fildor Nov 12 '14 at 10:03
  • Splash screen code posted above. Your help is very much appreicated –  Nov 12 '14 at 10:06
  • Post your manifest. The standard default behaviour is to return a user to a task with the same activity on top as it was when the task was pushed to the background. You don't have to do anything extra to get this behaviour. You must be doing something strange. – David Wasser Nov 12 '14 at 21:46
  • If you are starting your app from an IDE (Eclipse, Android Studio, etc.) or starting it from the installer, you may also be seeing this nasty Android bug: http://stackoverflow.com/a/16447508/769265 – David Wasser Nov 12 '14 at 21:50

1 Answers1

1

your solution is that whenever you entered in any new activity....

1.) You have to save the name of this activity in shared preferance(on same key) you have to perform this things in onResume() of every activity or your BaseActivity.

2.) At the every time of splash screen you have to check the name of this activity and pass it in your intent.

so you get the last open activity each time after your splash screen done.

Community
  • 1
  • 1
GovindRathod
  • 867
  • 1
  • 8
  • 22
  • What OP wants is standard, default Android behaviour. He doesn't have to jump through programming hoops to get it. Sometimes you need to read between the lines. This may be an answer to the question, but it isn't actually what OP wants or needs. – David Wasser Nov 12 '14 at 21:52
  • @DavidWasser so you mean to say that i should include the code with the answer right ? – GovindRathod Nov 13 '14 at 04:15
  • No. What I'm saying is that this isn't a helpful response to OP's problem. It may answer the question he asked, but isn't what he needs. If OP (or anyone else reading this) follows your example, they are going down the wrong path (IMHO). Just my 2 cents. – David Wasser Nov 13 '14 at 10:37