android - How to start on current activity when user presses Home button -
on main activity added following intent-filter
<intent-filter> <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>
so when press home button main activity showed. last seen activity show, not main one. how can achieved?
store each activity in sharedpreference through onpause , can rectify it.
@override protected void onpause() { super.onpause(); sharedpreferences prefs = getsharedpreferences("x", mode_private); editor editor = prefs.edit(); editor.putstring("lastactivity", getclass().getname()); editor.commit(); }
in mainactivity:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); class<?> activityclass; try { sharedpreferences prefs = getsharedpreferences("x", mode_private); activityclass = class.forname( prefs.getstring("lastactivity", activity1.class.getname())); } catch(classnotfoundexception ex) { activityclass = activity1.class; } startactivity(new intent(this, activityclass)); } }