0

I don't know how to descibe this. I have two activities full of links (to other activitys) so like a menu or list. Basicly I can split the links in "physics" and "maths" (yes I need this for education). I made two links in my actionbar, like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
    <item 
        android:id="@+id/maths"
        android:title="@string/maths"
        android:orderInCategory="1"
        android:showAsAction="always|withText" 
        android:titleCondensed="@string/maths" 
        android:icon="@drawable/ic_action_calculator" /> 
    <item 
        android:id="@+id/physics"
        android:icon="@drawable/ic_action_line_chart"
        android:title="@string/physics"
        android:orderInCategory="2"
        android:showAsAction="always|withText"/>
</menu>

Everything is working great, I have like "tabs" on the bottom of my app. Following code is working too:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.maths:
            return true;
        case R.id.physics:
            Intent myIntent2 = new Intent(Home.this, ActivityPhysics.class);
            startActivity(myIntent2);
        default:
            return super.onOptionsItemSelected(item);
    }
}

As you can see, when clicked on "math" it does nothing, because I am already in maths.

When clicked on "physics" it's opening ActivityPhysics.

My problem is, that when I click on physics and go to physics, then click on math and go to math, and that many times, i have to press the back button a bunch of times, and it boes back to math and back to physics...(understand, what I mean? sorry, I'm german :D )

Yeah and that "back back back back" sucks :)

sabadow
  • 5,095
  • 3
  • 34
  • 51
Lesik2008
  • 487
  • 1
  • 8
  • 16

1 Answers1

0

Okay so I am posting this question directly with an answer, because while writing I got a genial idea :D I have seen the code for the "up" button (like in Gmail etc). Just replace the "go to home.java" to "go one up, and that's home.java".

So the system thinks, it's up instead of a link to another activity, it's like a "back" button :)

So you have to replace this:

Intent myIntent2 = new Intent(ActivityPhysics.this, Home.class);
startActivity(myIntent2);

With this:

Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

This worked for me, used minSdkVersion 14 :)

sabadow
  • 5,095
  • 3
  • 34
  • 51
Lesik2008
  • 487
  • 1
  • 8
  • 16