i'm developing a basic app that has four activities. The first two Activities have textviews that are reseted when the back button is pressed. here's the manifest
<activity android:name=".MainActivity"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContactActivity" />
<activity android:name=".Page2Activity"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance"/>
<activity android:name=".FinalActivity"></activity>
I've also used the android:freezesText="true" but it doesnt help
I didnt write any android programs. But you should try android:alwaysRetainTaskState:"false"
Whether or not the state of the task that the activity is in will always be maintained by the system — " true " if it will be, and " false " if the system is allowed to reset the task to its initial state in certain situations.
Your solution may be saving the data in string form and repopulating your object on resume. You would save the data in onPause() and repopulate in onResume().
I will show you how to do that with a SharedPreference. SharedPreferences are an easy way to save strings, integers, lists, and other objects, without a database in android.
//This code will save a string. The first parameter in putString() is the key. The second is the value
SharedPreferences.Editor editor = getSharedPreferences("myPrefs", MODE_PRIVATE).edit();
editor.putString("myTextviewText", "Hello World");
//Save the data
editor.apply();
//This code will retrieve the String. You can run this code and retrieve the value even if the app was killed
SharedPreferences prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
String restoredText = prefs.getString("myTextViewText", "default");
You can override onPause and in there use the first part of the code to save what you want in your textview. I'm onResume use the second part to retrieve the string and then edit the textview text with the restored text. To hold serveral values just use a different key to set and get data.
SharedPreferences will save when the app is stopped. They act like a database but with less maintenance.
Related
I want to build Terms and Conditions dialog display on a "startActivity". If the user clicked on the Accept button or clicked on a Checkbox, it start the app with MainActivity. But later, on second/third etc. start, i will skip the "startActivity", i will run the app direkt with the MainActivity. (if the user clicked earlier on the Accept button). How can this be solved?
Thanks!
You just need to:
Make sure your main activity is the one system starts
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In your Main activity check if user has already accepted terms or no. If no, drive him to TnC activity.
Obviously state of TnC acceptance must be stored somewhere (preferences, room, firebase etc).
Use SharedPreferences to store and access the user's response. https://developer.android.com/training/data-storage/shared-preferences#java
SharedPreferences sharedPref =
getActivity().getPreferences(Context.MODE_PRIVATE);
String res=sharedPref.getString("UserResponse","NONE");
if(res.equals("Acccept")){
//goto main activity here
}
//get user response into String response
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("UserResponse", response);
editor.commit();
What I originally had: a launcher activity with a button which would launch my main activity. The launcher activity had shared preferences so it would only show up once. However, I have added a new activity that when the user clicks the button on the launcher activity, a introduction page is to appear. However due to shared preferences on my launcher activity, that introduction page does not show up.
So is there any way to clear the user's data on update? Just for this update, When a user updates my app, I want this to run:
SharedPreferences settings = getActivity().getSharedPreferences("cda-preferences", Context.MODE_PRIVATE);
settings.edit().clear().commit();
Is this possible?
Yes, you can define a BroadcastReceiver for the MY_PACKAGE_REPLACED action. In this receiver's onReceive method you should clear the flag in the shared Preferences. (API12+)
<receiver
android:name=".OnUpgradeReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
Or you should save the version of your app (to the shared prefs). When the user launches the app you should check what was the previous one and if its newer, show the introduction page.
I have developed an application which makes use of a toggle button to decide the enabling/disabling of my app. I store the toggle button state as a static variable so that it retains it value on stop and resume. However on a reboot, the static variables will be reinitialised into its default state. Is there any way I can get my app to resume its state even after a reboot?
Specifically, what my app does is that on toggle On it enables a "Service". SO I want that service to be started automatically on a phone reboot. Is that possible?
Thanks
save the variables in SharedPreferences
see how it works, http://developer.android.com/reference/android/content/SharedPreferences.html
then on start of your activity just restore them
not so important but if youre using toggle you may save boolean
tutorial: http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
GL
EDIT: on other Q about services, well dont really know what you made so far but maybe this: Android -Starting Service at Boot Time can help, if not provide some code what you made.
Well,that's possible when you your app starts as the phone reboots.Service will be called via main activity.
You can start the app as soon as the phone boots via a broadcastrecievir which looks like this .:-
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
This needs to be added in AndroidManifest as :-
<receiver
android:name="com.example.xyz.BootUpReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Store your variable into SQLite or SharedPreferences. Then restore it's state on Activity restart.
You can store your variables into sharedpreferences, this will be much easier
http://developer.android.com/reference/android/content/SharedPreferences.html
// to save it
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString("your_key", "your_value");
editor.commit();
//to retrieve it back
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
String your_value= prefs.getString("your_key", null);
I'm developing an Android app which requires the user to be logged in (actually, at the moment they just need to supply a username as no authentication is required... this will change in the future though).
I've managed to implement this, but I'm not sure if it's the best way. I have a LoginActivity which I have declared in the manifest using android:noHistory="true" and:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In LoginActivity's onCreate method, I check for a Boolean value isFirstStart stored in SharedPreferences. If the value does not exist, it must be the first start of the app, so I let LoginActivity set everything up, perform the login, and then launch MainActivity using:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(MainActivity.EXTRA_ISFIRSTSTART, isFirstStart);
startActivity(intent);
I use the extra so that I can open the navigation drawer in my main activity if it is the first start. At this stage I also store the Boolean value as false in SharedPreferences.
If isFirstStart already exists as false, I perform the launch of MainActivity straight away using the same method.
To me, this somehow doesn't seem like the most efficient method - launching LoginActivity on every startup even though it will not be used most of the time.
Any thoughts?
I have two activities, Activity1 and Activity2. Activity1 is launcher activity which starts at the beginning. Now when I'm pressing Home button from activity2 and again opening the app from all apps then Activity1 is opening. I want to open the same activity that was opened at the time of pressing the home button. I need to save state of activity2, but how I don't have any clue.
I looked at this and this but still I didn't got the clear picture on how to do this. Please help me as I'm new to android.
When you pressed "home" button, your activity goes to onPause().
So I personally recommend you to override the onPause() method which can not only handle the "home" button pressed but also other circumstances.
In your case, it is only onPause() and onResume() related, so you can try put the state into SharedPreferences or Internal/External storage.
say:
in your onPause() method, do something like:
// Use Shared Preferences to save data
SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
SharedPreferences.Editor prefEditor = previewSizePref.edit();
prefEditor.putInt("x", somethingA);
prefEditor.putInt("y", somethingB);
prefEditor.commit();
and in your onResume(), retrieve the saved data like:
SharedPreferences previewSizePref = getSharedPreferences("PREF",MODE_PRIVATE);
if (previewSizePref.contains("x") && previewSizePref.contains("y")) {
//your saved data exists, do something
} else {
// handle the circumstances that the saved data doesn't exist
}
For saving state in onPause() and restore in onResume() you can have a look at this answer:
Saving Activity State in the onPause
And for SharedPreferences, you can have a look at API document: Data Storage - Shared Preferences
What you're describing should be standard for behaviour for Android, and if it isn't doing what you describe then you've probably overwritten this behaviour somewhere else, probably in your manifest.
If you create a new Android application and make a simple two page app, with Activity A as the launcher and Activity B as the second page, then your manifest will look like the following and the app will display the behaviour you describe.
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ActivityA"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActivityB"
android:label="#string/title_activity_main" >
</activity>
</application>
Double check that your manifest looks similar to this- Activity B has no flags and is just a declaration that it exists in your manifest.