I'm trying to implement time out notification. Here is what I want to do. If app goes to the background mood, after 5 minutes it should warn the user that app will be signed out. After this notification, if user opens the app, it should ask for the password, if user inputs correct pass, it should retrieve the screen that he left 5 mins ago. My problem is that I cannot save the activity that user left in order to retrieve after they input password. Here is what I've done so far:
I added a countdown inside of onSaveInstanceState() method, and create an instance of current intent. Then in TimeOutActivity I defined a property called savedActivity in order to pass the current activity in that instance.
private boolean isTimedOut;
private TimeOutActivity timOutActivity;
private CountDownTimer timer;
@Override
protected void onSaveInstanceState(Bundle state)
{
super.onSaveInstanceState(state);
if (timOutActivity == null)
{
timOutActivity = new TimeOutActivity();
timOutActivity.setIntent(new Intent(getApplicationContext(), TimeOutActivity.class));
**timOutActivity.savedActivity = this;**
initializeTimer();
}
}
private void initializeTimer()
{
timer = new CountDownTimer(60000, 1000)
{
public void onTick(long millisUntilFinished)
{
if (millisUntilFinished / 1000 == 55)
{
showSignOutWarning();
isTimedOut = true;
}
}
public void onFinish()
{
// Push local notification
isSignedOut = true;
}
}.start();
}
Then In onStart() method I added following code:
@Override
public void onStart()
{
super.onStart();
if (isTimedOut)
{
// Load timedOut activity
startActivity(timOutActivity.getIntent());
isTimedOut = false;
}
}
But when I want to retrieve the activity inside of TimeOutActivity, my property (savedActivity) is null. What am I doing wrong? Is there a better way to save the current activity?
Update Oct 31 2014
I defined a class called: App.java that extends Application. By this class I can define global variables with their setters and getter. It's like bellow
public class NGVApp extends Application
{
private WebView webView;
public WebView getWebView()
{
return webView;
}
public void setWebView(WebView webView)
{
this.webView = webView;
}
}
Then in AndroidManifest.xml in Application tag I added this
android:name="your.app.bundle.name.App"
Then in my WebViewActivity onStop() method, I set the webView instance like bellow:
((App) this.getApplication()).setWebView(webView);
And finally in onCreate() method I added:
@Override
protected void onCreate(Bundle savedInstanceState)
{
if(((NGVApp) this.getApplication()).getSavedInstanceBundle() != null)
{
savedInstanceState = ((NGVApp) this.getApplication()).getSavedInstanceBundle();
}
}
I could successfully get the instance of webView and it's not null anymore but it's not containing the webView contents (The one that user interacted before time-out) any idea?
See Question&Answers more detail:
os