android - Keep an activity/activities alive while the app is open in the background -
hey guys i'm brand new site, know people put down lot asking duplicated questions hope isn't 1 of them.
as site, new android programming , have created simple app calculating price based on time.
this app has 3 activities use switch layouts 1. firstactivity
- shows title page , button launch second activity 2. secondactivity
- starts timer , calculates price based on seconds. 3. lastactivity
- shows end results of time , price , allows user share other apps (email, sms)
everything working fine, when phone orientates current activity killed , restarted i've fixed forcing layout portrait - i've run problem again when move app background
so actual question: need secondactivity
keep time, , lastactivity
keep saved info if user switches apps. i'm not talking when app closed im talking when app switcher or home button activated , app still open in background.
thank in advance!
so in end needed more information on how implement data save features suggested , android has, found answer needed here: how make android app return last open activity when relaunched?
to solve problem needed add: |1| new activity
|2| <action>
, <category>
tags activity |3| sharedpreference
setters in each activity needed keep.
per @josefpfleger's intructions made new blank activity named
restoreprevactivity
, in android manifest setmain
,launcher
,default
removed intent filters in first activity defaulted there after creating app first time.<activity android:name=".firstactivity" android:label="@string/app_name" android:screenorientation="portrait" > </activity> <activity android:name=".secondactivity" android:label="@string/title_activity_second" android:screenorientation="portrait" android:launchmode="standard"> </activity> <activity android:name=".lastactivity" android:label="@string/title_activity_last" android:screenorientation="portrait" android:launchmode="standard"> </activity> <activity android:name=".restoreprevactivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity>
after setting action , category of new activity needed add code activity. added
oncreate()
ofrecoverprevactivity
`
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); class<?> activityclass; try { sharedpreferences prefs = getsharedpreferences("x", mode_private); activityclass = class.forname( prefs.getstring("lastactivity", fareactivity.class.getname())); } catch(classnotfoundexception ex) { activityclass = fareactivity.class; } startactivity(new intent(this, activityclass)); }
where firstactivity
replaced default activity run if no previous activity found.
- finally went through activities , added
onpause()
saved activity name.
i added following code segment other activities , working flawlessly.
@override protected void onpause() { super.onpause(); sharedpreferences prefs = getsharedpreferences("x", mode_private); sharedpreferences.editor editor = prefs.edit(); editor.putstring("thisactivity", getclass().getname()); editor.commit(); }
where thisactivity
changed name of current activity.
result able launch app, start timer , press home button or app switcher safely switch apps or lock screen , upon return brought timer running uninterrupted! put test , ran clash of clans, memory heavy game , watched youtube videos 30 app running in background , came app see time correct , ran while gone.
thanks everyone's help! -stephen
Comments
Post a Comment