Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
754 views
in Technique[技术] by (71.8m points)

android - Retrieve an activity after Time out warning notification

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...