How to start activity twice with different layout and not keep it in activity back stack in Android -
i have activities flow:
+--+ +--+ +----+ |a1|-->|a2|-->|home| +--+ +--+ +----+
back button in a1 , home should close app. call finish()
on a1 when a2 started , on a2 when home started (thanks a1 , a2 never kept on stack).
however, button in a2 should lead a1 override in a2 (a1 finished stated above have start again)
@override public void onbackpressed() { final intent intent = new intent(getapplicationcontext(), a1.class); startactivity(intent); finish(); }
now, it's possible start again button in home sequence
home (button pressed)->a1->a2->(return home)
in case (i.e. when a1->a2 started after home) layout of a1 should little bit different.
you need use action, instead of action. read first understand difference http://developer.android.com/training/implementing-navigation/ancestral.html
also, how skip a2, home:
manifest
<activity android:name="com.yourpackage.a1"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <activity android:name="com.yourpackage.a2" android:parentactivityname="com.yourpackage.a1" > <!-- parent activity meta-data support 4.0 , lower --> <meta-data android:name="android.support.parent_activity" android:value="com.yourpackage.a1"/> </activity> <activity android:name="com.yourpackage.home" android:parentactivityname="com.yourpackage.a1" > <!-- parent activity meta-data support 4.0 , lower --> <meta-data android:name="android.support.parent_activity" android:value="com.yourpackage.a1"/> </activity>
in a2 , in home:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // activate navigation in actionbar getsupportactionbar().setdisplayshowhomeenabled(true); getsupportactionbar().setdisplayhomeasupenabled(true); //other stuff } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { // respond action bar's up/home button case android.r.id.home: homeupaction(); return true; } return super.onoptionsitemselected(item); } private void homeupaction() { intent upintent = navutils.getparentactivityintent(this); if (navutils.shoulduprecreatetask(this, upintent)) { // activity not part of app's task, create new // task when navigating up, synthesized stack. taskstackbuilder.create(this) // add of activity's parents stack .addnextintentwithparentstack(upintent) // navigate closest parent .startactivities(); } else { // activity part of app's task, navigate // logical parent activity. navutils.navigateupto(this, upintent); } }
Comments
Post a Comment