I'm creating an app that loads my website to a webview. I want to prevent that page reloads when I came back to app. If I change to other app or power off and came back the page shows without refresh, the problem occurs only it stays off for a few minutes. I think that the application change the state to stop or is destroyed and when I enter again is called the method oncreate.
I've tried to change android:launchMode to singleTop an singleTask without success, is the creation of a service the solution? Thanks in advance.
In order to prevent refreshing of WebView, you need to save and restore state.
This is done by overriding lifecycle methods onSaveInstanceState and onRestoreInstanceState
#Override
protected void onSaveInstanceState(Bundle outBundle)
{
super.onSaveInstanceState(outBundle);
webView.saveState(outBundle);
}
#Override
protected void onRestoreInstanceState(Bundle savedBundle)
{
super.onRestoreInstanceState(savedBundle);
webView.restoreState(savedBundle);
}
Furthermore, you can try to set cache on the WebView instance.
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
webView.loadUrl(url);
Related
I have an application which has a main activity. When it switches to a second activity I do it like this :
startActivity(intent);
finish();
I was hoping this would fully prevent the application from going back to the main activity. When I press the android back button in the secnond activity it goes back to the home screen (which is what I want). But when I open the app again from multitasking it goes back to the first activity! Has anyone run into this issue before?
I was running into this problem, where my first screen would be a login activity that's only meant to show if the user is not logged in. Otherwise, it show the home screen. I tried out SharedPreferences for login persistence, but it turns out the authentication service I'm using, Google Firebase, has built-in login persistence.
Without more information, however, I can only suggest that you research SharedPreferences and have a boolean check a condition upon start-up. If the condition is met, start the home activity; otherwise, show the main activity. Something like:
boolean isLogin;
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = getApplicationContext().getSharedPreferences("MyPrefs", MODE_PRIVATE);
isLogin = settings.getBoolean("islogin", false);
if(isLogin) {
finish();
goToDash();
}
Public Interface Shared Preferences, Android Developers
Obtaining Shared Preferences
Working with Android Shared Preferences
Figured it out! My application uses a twitter api, and relaunching the application would cause an error as it retried to get credentials it didn't have before. I also made sure to completely clear the stack when switching certain applications by doing:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
When restarting an Activity, onStop() from previous activity delays too much to be called.
I am using this code to restart my activity PlayerActivity.java
Intent playerIntent = getIntent();
playerIntent.putExtra(Constants.VIDEO_ID, videoId);
playerIntent.putExtra(Constants.CATEGORY_ID, categoryId);
playerIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(playerIntent);
Let's call PreviousPlayerActivity and NewPlayerActivity, the activity that was before and the new activity. (remembering that they are the same PlayerActivity).
Sequence
When restarting the app follows this flow in the activity-lifecycle.
PreviousPlayerActivity onPause() -->
NewPlayerActivity onCreate() -->
NewPlayerActivity onStart() -->
NewPlayerActivity onResume() -->
NewPlayerActivity performs a heavy operation -->
PreviousPlayerActivity onStop() -->
PreviousPlayerActivity onDestroy()
What I need
I need PreviousPlayerActivity to be completed destroyed before NewPlayerActivity starts. However, onStop() is just called after the heavy operation, so it delays around 10 seconds to be called.
What I tried
If I use recreate() method it does destroy PreviousPlayerActivity before calling NewPreviousActivity, but by calling recreate() I can not putExtras into the new activity instance.
Questions
How to completely destroy PreviousActivity when restarting an activity?
Is there a way to putExtras while using recreate()?
In Activity Lifecycle from Android Developer guide.
Coordinating activities
When one activity starts another, they both experience lifecycle
transitions. The first activity stops operating and enters the Paused
or Stopped state, while the other activity is created. In case these
activities share data saved to disc or elsewhere, it's important to
understand that the first activity is not completely stopped before
the second one is created. Rather, the process of starting the second
one overlaps with the process of stopping the first one.
The order of lifecycle callbacks is well defined, particularly when
the two activities are in the same process (app) and one is starting
the other. Here's the order of operations that occur when Activity A
starts Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen, its onStop() method executes.
This predictable sequence of lifecycle callbacks allows you to manage
the transition of information from one activity to another.
So the behavior that you describe is expected or predictable.
Back to your questions.
1.How to completely destroy PreviousActivity when restarting an activity?
Using recreate API, the limitation is it only works from API 11 or above
2.Is there a way to putExtras while using recreate()?
From recreate documentation
recreate
public void recreate ()
Cause this Activity to be recreated with a new instance. This results
in essentially the same flow as when the Activity is created due to a
configuration change -- the current instance will go through its
lifecycle to onDestroy() and a new instance then created after it.
Because the activity will be recreated so onSaveInstanceState and onRestoreInstanceState will be called as well. As you can guess the idea is save data in onSaveInstanceState and retrieve in onRestoreInstanceState or onCreate.
Step 1: Save data in onSaveInstanceState
// The key for saving and retrieving isActivityRecreated field.
private static final String KEY_IS_ACTIVITY_RECREATED = "KEY_IS_ACTIVITY_RECREATED";
/** true if this activity is recreated. */
private boolean isActivityRecreated = false;
// Call this method when you want to recreate this activity.
private void recreateActivity() {
isActivityRecreated = true;
recreate();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_IS_ACTIVITY_RECREATED, isActivityRecreated);
outState.putInt(Constants.VIDEO_ID, videoId);
outState.putInt(Constants.CATEGORY_ID, categoryId);
}
Step 2: Retrieve the data in onRestoreInstanceState or onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
isActivityRecreated = savedInstanceState.getBoolean(KEY_IS_ACTIVITY_RECREATED);
if (isActivityRecreated) {
// This activity has been recreated.
// Reset the flag
isActivityRecreated = false;
// Write your code when this activity recreated.
int videoId = savedInstanceState.getInt(Constants.VIDEO_ID);
int categoryId = savedInstanceState.getInt(Constants.CATEGORY_ID);
...
}
}
}
You can do it simply by setting a noHistory flag as true in AndroidManifest.xml file. I think you don't need to keep that activity state when minimizing according to your requirement.
<activity
android:name=".PlayerActivity"
android:noHistory="true" />
I am creating an app. From MainActivity -> Fragment -> See Details Activity.
It is working fine.
But when I am going to background on See Details Activity, it is still running.
Why this activity or app is not getting destroyed. I want to kill the app whenever I will go to background.
Add this:
#Override public void onPause() { super.onPause(); // Always call the superclass method first
Finish();
}
Invoke finish() method before start Fragment
I'm looking for a way to close an android app after some time while the app has not been in focus. For example if the user open up an other app instead, the app should exit after 5 mins. I have tried using runnable and creating a thread. But those method don't seems to work while the app is in the background (maybe they are pause I'm not sure). So how do I close the app when it is not in focus?
For those who are wonder the reason I want to do this is that the app contains some sensitives data about the user so I want to be sure it is all cleared when they aren't using it.
Something like this might work:
A field inside activity class:
private Thread t = null;
Inside onResume():
if(t!=null) {
if(t.isAlive()) {
t.interrupt();
t.join();
}
t=null;
}
Inside onPause():
t = new Thread() {
public void run() {
try {
sleep(5*60*1000);
// Wipe your valuable data here
System.exit(0);
} catch (InterruptedException e) {
return;
}
}.start();
}
I recommend calling finish() in the onPause() or onStop() callbacks. A TimerTask will not survive onPause() and a Service does not appear, on face value, to give you options. Maybe you can start a service, sleep the thread the service runs on, then kill the processes your app has after the sleep timer expires.
Alternatively, you can just implement some security libraries to help secure the data from other apps.
Here is the Google Services link.
Get the process ID of your application, and kill that process onDestroy() method
#Override
public void onDestroy()
{
super.onDestroy();
int id= android.os.Process.myPid();
android.os.Process.killProcess(id);
}
Refer- how to close/stop running application on background android
Edit- Use this with AlarmManager
The fundamental problem with what you're trying to do is that your Activity may not exist in memory at all when it's "running" in the background. The Android framework may have destroyed the activity instance and even the process it was running in. All that exists may be the persistent state you saved in onSaveInstanceState(...) and a screenshot for the recent apps list. There may be nothing for you to get a reference to and kill.
Frank Brenyah's suggestion to call finish() in onPause() will prevent your activity from running in the background at all, but this is the closest you can get to what you want. You probably only want to do this when isChangingConfigurations() is false. But even when all your app's activities are finished, Android may keep the process and Application instance around to avoid recreating them later. So you may also want to use Bhush_techidiot's suggestion of killing the process. Do this in onPause() because the activity may be destroyed without a call to onDestroy().
Hi I have an App which has a strict requirement of appearing "logged in" even following the app being destroyed. If the App was destroyed then later re-loaded (if previously logged in) login screen should be bypassed > directly to the page the user was previously viewing. (I already handle all session related variables)
I've tried using Shared Preferences but if I destroy the App manually it would go back to the login screen, I can't have this happen.
I tried storing preferences manually in the DB (which is probably what Shared Preferences does anyway?) but it still loads the Login Activity of course because this is my first loaded activity. So this problem is more a case of keeping a preference on which Activity the user is on at all time then loading directly to this onResume().
My question: Has anyone dealt with this sort of scenario before? How should I approach pre-loading the Activity. I was thinking that when I load my preferences within the Login screen I check the previous Activity preference and simply load into that (assuming user is logged in).
Is there a better way to approach this? Is there a more native way of loading dynamically to appropriate Activity start?
Any help is really appreciated
Edit: Ok just after posting this I realised all I really had to do was add a check in OnResume whether the appropriate session variable was set. then load into the Activity, actually just as Akbari says below. I've rolled my Preferences class back to using the standard Android SharePreferences and its working perfectly now. happy about that :)
Simply doing something like this:
// load preferences
preferences.readPreferences();
// if preferences exist load straight to RecordActivity
if (application.currentSessionId!=null) {
Intent i = new Intent(getApplicationContext(), RecordActivity.class);
startActivity(i);
}
you can save login status in preferences and check it in onCreate() method of your Login activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);SharedPreferences prefs;
prefs = getSharedPreferences("your_pref", MODE_PRIVATE);
boolean login_status = prefs.getBoolean("login_status", false);
if (login_status) {
Log.v(LOG_TAG, "UserInfo>>User already logged in");
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
this.finish();
}
}
Here, it will check login status and redirect user to next activity if already logged in