Michał Tajchert
Michał Tajchert

Reputation: 10383

Pressing back button in child activity-> main activity in app?

I'm using SlidingMenu and 3/4 navigation is done by it. And if I want to go back I have to went through all these activities which I open before. I would like to change it to such that closing any child activity (not a main activity) sends me to app main activity, and if then I press back it exit app. Simple as that.

I open new activities that way:

Intent LinieActivity = new Intent(Oznaczenia.this, Linie.class);
            startActivity(LinieActivity);

and I have tried putting in my Manifest such thing:

<activity
        android:name=".childActivity"
        android:label="some name">
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.MainActivity" />
    </activity>

Upvotes: 0

Views: 1689

Answers (2)

Mike
Mike

Reputation: 413

Once you are done with an activity, you can call the command finish(); to close that page. So I would call the next page I want to open using an intent, then after that call finish(); and the page will close.

ItemPage ItemPage = new ItemPage();
Intent i = new Intent(context, ItemPage.class);
startActivity(i);
finish();

When you hit the back button, none of the pages you called finish on will be there.

Upvotes: 4

androidgeek
androidgeek

Reputation: 3480

Hey here is the code for back button pressed you can mange if you want to exit you can otherwise you can go to your main activity

   @Override
    public void onBackPressed() {
    new AlertDialog.Builder(this)
    .setTitle("Exit App?")
    .setMessage("Are you sure you want to exit?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            WelcomeActivity.super.onBackPressed();
        }
    }).create().show();
    }

Upvotes: 0

Related Questions