1

I'm using the following code to switch between activities:

Intent b = new Intent(nowActivity.this, About.class);
startActivity(b);
return true;

This code works perfectly but it seems to make it start a new activity on top of the existing activity.

Example: I start the application on my Android phone and pressing the menu button to make the optionsmenu up on the screen. I then hit "About" to go to the about page for the application. If I then pressing the "Close" option in the menu, the first page (home screen of the application) will be visible on the screen and everything is back to square 1, just like I was opening the application once again.

I don't want it to act like this and I asking you now, how can I fix this problem?

Thanks in advance.

Airikr
  • 6,258
  • 15
  • 59
  • 110

3 Answers3

1

I got it working! Here's the solution:

nowActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.menuItem1:
        Toast.makeText(this, "Du är redan på startsidan", Toast.LENGTH_SHORT).show();
        return false;

    case R.id.menuItem2:
        Intent b = new Intent(nowActivity.this, About.class);
        b.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(b);
        return true;

    case R.id.menuItem3:
        android.os.Process.killProcess(android.os.Process.myPid());
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

About.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuItem1:
        Intent a = new Intent(About.this, nowActivity.class);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(a);
        return true;

    case R.id.menuItem2:
        Toast.makeText(this, "Du är redan på \"Om\"-sidan", Toast.LENGTH_SHORT).show();
        return false;

    case R.id.menuItem3:
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        System.exit(0);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}
Airikr
  • 6,258
  • 15
  • 59
  • 110
0

edit your code: call finish() to destroy the first activity

Intent b = new Intent(nowActivity.this, About.class);
startActivity(b);
finish();
return true;
Nishant
  • 32,082
  • 5
  • 39
  • 53
  • Thanks but I don't want to close the activity when switching to a new activity. When I'm hitting the back button on my phone, the application will go back one step (from "About" to "Home" for example). If I press the same button once again, the application will close unless I choose "Close" first. – Airikr Apr 05 '12 at 04:22
  • Thank you :) Those answers was kinda neat but see my comment for hopia's answer. – Airikr Apr 05 '12 at 04:42
0

How about going back to the Home screen when the user clicks on your "Close" button?

The following code is taken from: How to return to home screen from Activity

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Community
  • 1
  • 1
hopia
  • 4,880
  • 7
  • 32
  • 54
  • Thanks! That solved my issue a little. When I'm pressing the back button on my phone the application closes, even if the application should go back to "Home". – Airikr Apr 05 '12 at 04:42