質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top