0

There are a activities in my application. Lets call them activities 1,2,3. All of them child to MainActivity. In my application I define for each child activity:

    <activity
        android:name="SettingsActivity"
        android:label="@string/title_gen_activity_settings"
        android:parentActivityName="MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="MainActivity" />
    </activity>

In onOptionsItemSelected it handled:

    else if(item.getItemId() == R.id.set_general_settings){
        Intent intent = new Intent(this, GeneralSettingsActivity.class); 
        startActivity(intent);
    }

However if I navigate for example from main->1->2->3 and then press up button it return me 3->2->1-> main. It does not return me to 3->main. What can be the reason?

Jacob
  • 1,135
  • 5
  • 14
  • 29
  • Anymore codes? Just this? – Raptor Mar 04 '14 at 03:32
  • Is any other code may be relevant? – Jacob Mar 04 '14 at 03:35
  • I added the code from onOptionsItemSelected. I just not sure what code may be relevant for that. – Jacob Mar 04 '14 at 03:44
  • @Jacob Unless I'm mistaken, this appears to be the code for how you are getting to the activity, not going back up from. If you post the code for how you're going back, then we could probably figure out why it isn't going to the parent activity. – T3KBAU5 Mar 04 '14 at 03:46
  • I do not handle it. May be this is a problem. I do not know what and where to handle. Do you have any example to point me? – Jacob Mar 04 '14 at 03:51
  • @Jacob Can you clarify one more thing? Are you wanting the hardware back button to go to the parent activity or the home/up in the actionbar? – T3KBAU5 Mar 04 '14 at 03:57
  • it is hardware button – Jacob Mar 04 '14 at 03:58
  • @Jacob Okay, let me fix my answer with what I know. (done) – T3KBAU5 Mar 04 '14 at 03:59

1 Answers1

1

To handle the hardware back button, you can use this code (found on How to handle back button in activity)

@Override
public void onBackPressed() {//for api level 5
    // your code.
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {//higher than api 5
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Use the first one if you have your minimum API set at level 5 or lower (as far as I can tell that should work)

Now, we need to navigate up. So, where the your code comment is, you should be able to write:

NavUtils.navigateUpFromSameTask(this);

and have it navigate up properly.

Community
  • 1
  • 1
T3KBAU5
  • 1,861
  • 20
  • 26
  • I do not need back. That how it works now. I need up. So every child will go to main. – Jacob Mar 04 '14 at 03:38
  • 1
    @Jacob you should post these codes in your question – Raptor Mar 04 '14 at 03:40
  • @Jacob, ah, my misunderstanding. If you post your full code I could be of more help. – T3KBAU5 Mar 04 '14 at 03:41
  • @T3KBAU5 Thank you. I'm trying this and will update if it works. Could you please explain what is the point mention parent in AndroidManifest.xml if we need handle it in code? – Jacob Mar 04 '14 at 04:20