Retrieve an activity after Time out warning notification
Asked Answered
D

2

2

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?

Dworman answered 1/10, 2014 at 13:13 Comment(7)
When app gets killed in background, your savedinstance also will become null. It is better to use SharedPreferences to do this and save the last activity name in it and retrieve from there .Dott
Thanks @BharathMg If I just save the activity name, it can retrieve the whole activity? would you please explain more?Dworman
You can use Intent intent = new Intent().setClassName(context,targetClassString); to convert string class name to an intent.Dott
targetClassString is the name of an Activity you should save in SharedPreferencesDott
@BharathMg In my activity, user may added some text or changes the background color, would this method saves these along with the activity name?Dworman
No.. It just saves the Activity name which you want to show when the user comes back. If you want to persist the changes in the view, you need to add those also in SharedPreferences so when you open the activity, fetch the changed values from SharedPrefs and apply it..Dott
@BharathMg This is the problem, If I want to just show the new instance of activity, I don't even need the sharePreferences, I just initialize a new activity. I have some complecated views in my activity that cannot be saved in sharedPreferences, I have no option but saving the current status of instance.Dworman
D
1

Finally I figured out the solution. There is no way to save the state of WebView and have all of the content, so we need to prevent reloading or recreating the webView. In order to have one instance of our activity that contains webView we should add the following code to our Manifest file:

<activity ...
        android:launchMode="singleInstance"
        android:alwaysRetainTaskState="true">

Then if through your application you needed to recreate the instance entirely to refresh the webView, you can use finish(); if it's inside of the activity or if you are in another activity and you want to recreate your webView activity, read this link to do it.

Dworman answered 29/1, 2015 at 17:5 Comment(0)
M
0

First of all, you should use the AlarmManager to trigger your notification from a Service.

Intent intent = new Intent(context, YourNotifService.class);
PendingIntent pendingService = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long triggerTime =  SystemClock.elapsedRealtime()  + (5 * 60 * 1000);
alarm.set(AlarmManager.ELAPSED_REALTIME, triggerTime, pendingService);

About saving the complete state of your Activity, that's really up to you to go through each View and save the state. Best way would be to save it in a specific file and not in the SharedPreferences, but both could work.

Motmot answered 2/10, 2014 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.