Android - How to start the exact same activity every time the app is opened up?

前提是你 提交于 2019-12-18 09:39:55

问题


In a nutshell, to give you an example, I basically have an app with 3 activities: Activity1 Activity2 StartActivity

StartActivity contains two buttons that correspond to the other two activities respectively, starting them up. If I exit the application from Activity1, when I later click on the app icon from the phone, Activity1 is restarted since Android keeps track of this. I need to have the app restart to bring me to the StartActivity, so that I can choose where to go by clicking the buttons, instead of having to click the back button to be able to end up at the StartActivity. I'm assuming onResume and onRestart are involved, but where should they go?

Any help is greatly appreciated thank you.


回答1:


You might look at the android:clearTaskOnLaunch activity attribute from the Manifest file : http://developer.android.com/guide/topics/manifest/activity-element.html

I think setting this attribute to "true" on your root activity does what you want.




回答2:


I would think the solution to be destroying Activity1 and Activity2 onStop. This leaves the stack with only your StartActivity. You can call the finish method in Activity to terminate it programmatically at anytime.




回答3:


Generally the way it works (from what I understand) is you use the BACK button to EXIT the activity, and the HOME button will essentially "minimize" the activity - bringing you back to whatever activity was left open.

While you should leave this functionality the same, you can override the home button to completely exit your application.




回答4:


In your mainfest.xml file write like this.

<activity android:name=".StartActivity" android:label="@string/app_name"
                android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>  

android:clearTaskOnLaunch="true" attribute automatically restart the activity.



来源:https://stackoverflow.com/questions/7046803/android-how-to-start-the-exact-same-activity-every-time-the-app-is-opened-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!