-1

I want my main activity to show every time after home screen is pressed and the app is opened again.

My layout is:

Main Screen --> Menu Page --> Sub Menu

I want to be able to use the back button to go from the sub menu to the menu page. However, regardless of which page I am on when the home screen is pressed I want to show the main screen when the app is reopened. I have tried onFinish() in my java class for MenuPage. That causes the return button to close the app because the previous activity was finished. I have researched how I can detect if the home button is pressed to finish the activity that way but it seems that's not an option. Does anyone know a way around this?

th3ramr0d
  • 484
  • 1
  • 7
  • 23
  • how many activities do you have?? – Tasos Nov 01 '15 at 13:38
  • 3. Main activity, menu page activity, sub menu activity – th3ramr0d Nov 01 '15 at 13:39
  • Main Activity is always Active (Should remain Active all the time) do you finish this for some reason? even if you press the back button on the Main Activity it will only minimize the App unless you specify otherwise with a dialog to say (do you want to close the App) – Tasos Nov 01 '15 at 13:44
  • So you want to do it just like snapchat? If you press home and back to the app, it goes to the camera part, regardless to where you were. – FirstOne Nov 01 '15 at 13:45
  • My main activity is already my default. Main activity is a splash screen. I want the splash screen to show every time the app is opened. If I do not call finish() then the app resumes where it was paused. I don't want this. If I call finish on the activities then I cannot return to them with the back button which I also do not want. – th3ramr0d Nov 01 '15 at 13:46
  • I haven't used snapchat but yes that sounds like what I want. – th3ramr0d Nov 01 '15 at 13:47
  • 1
    Then restart the Main Activity Intent when you press the home button (Intent intent = mainActivityClass; finish(); startActivity(intent);) that will show you the spash screen – Tasos Nov 01 '15 at 13:49
  • http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly – Jamil Nov 01 '15 at 13:56

2 Answers2

1

You can use FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET flag when starting your secondary activities. More information about intent's flags can be found here: link

Georgy
  • 364
  • 2
  • 14
0

To control your back presses, you can basically direct each activity or fragment to go wherever you which on back press by overriding the backpress event.

@Override
public void onBackPressed() {
    // TO DO
}

To also handle backpress and state with fragments, it is advisable to manage the back stack.

Providing Proper Back Navigation

I also use a custom home button on my apps when I want the user to be able to go straight back to the main activity.

public void goHome() {
    startActivity(new Intent(this, MainActivity.class));
}

edit
this was part of my original answer, but it was not addressing what the OP was asking. I left it here in case it helps any browsers who may also have misinterpreted the question and are looking for something like this.

You need to set your main activity to be the launcher activity for the app and then use BOOT_COMPLETED and DEFAULT. This means the user will have to choose a default option each time they press the home key, until they either set the app or the default device homescreen to 'always' be the default.

<activity android:name=".Main">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
  • I am having a hard time articulating my problem. I do not want the home button to return to main screen. Home button should close the application and then upon reopening the app I want to go straight back to the home screen instead of where the last activity was paused. So the first part of your answer is not what I am trying to do. However, onBackPressed solves it. I just call finish() on all activities and then onBackPressed recall the previous activity that was finished. Now home closes the activity completely but I can still use the back button to return to previous activities. – th3ramr0d Nov 01 '15 at 14:04