Activities interaction - android

I have 3 Activities, let the Activity A is the main Activity from which B and C Activities are called. B is Activity with settings, and C is Activity with the main actions. I need to realize ToggleButton in B which is responsible for whether the device will vibrate after pressing the buttons in C. Thus, it is necessary to connect B and C. If using Intent, it is necessary to call the StartActivity (Intent)/StartActivityForResult (Intent) method. From this it follows that when pressing ToggleButton in B, the C will be called by B. And it is unnecessary to me. I need that when pressing ToggleButton "something" was remembered "somewhere", and then when the C is called it will be cause to device vibrating. How to solve this problem?

You can use SharedPreferences to store some states, eg:
SharedPreferences prefs = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("key_string", "jack");
editor.putInt("key_int", 30);
editor.putBoolean("vibrator", false);
editor.commit();
Then you can read it when you need it:
SharedPreferences prefs = getSharedPreferences("myprefs", Context.MODE_PRIVATE);
String name = prefs.getString("key_string", "defaultName");
int age = prefs.getInt("key_int", 25);
boolean vib = prefs.getBoolean("vibrator", true);
Basically, SharedPreferences store key-value pairs. Read more about them here and here.

SharedPreferences is probably the best way to handle this. In Activity B create a SharedPreferences for vibrate, set to the boolean value "true" when the toggle is pressed, and then in Activity C check the SharedPreferences for said value and act accordingly.

Related

How to save the state of fragment (Android) when application is closed and restore it when application is run again?

I have two buttons on a fragment, "start" and "finish". when "start" is pressed, it gets hidden and "end" is shown. when "end" is pressed "start" is shown. i want to retain the state when app is closed and run again
I would recommend using SharedPreferences to achieve this. They are meant to be to retain app state when run at a later time.
In your case, you need to use putBoolean() method from SharedPreferences.Editor something like this:
"Start" button onClickListener:
SharedPreferences sharedPref = context.getSharedPreferences(context, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.editor();
editor.putBoolean("isJobRunning","true");
editor.commit();
In the same way, you set the isJobRunning to false once "End" button is called.
I would also move this code to a dedicated method something like this:
private void updateJobStatus(boolean isJobRunning) {
SharedPreferences sharedPref = context.getSharedPreferences(context, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.editor();
editor.putBoolean("isJobRunning", isJobRunning); // difference is visible here!
editor.commit();
}
On the onCreateView() method on the Activity, you can check if the job is running this way:
SharedPreferences sharedPref = context.getSharedPreferences(context, MODE_PRIVATE);
sharedPref.getBoolean("isJobRunning", false); // Here, `false` is the default value if the key is not located in `SharedPref`

Shared Preference data not cleared when activity is closed

in my application, I used place picker.and data that place picker gave is sent to 3 different activities using shared preference.and show this data in TextView.problem is when I closed activity and again open that activity my data still visible in TextView.even when I cleared it in onDestroy().
here is my code for send data from place picker:
SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("city_address", (String) cityAddress);
editor.putString("city_name", (String) city);
editor.commit();
Intent intent = new Intent(this, CleanlinessActivity.class);
startActivity(intent);
set data using this code in onCreate() of CleanlinessActivity
SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
String n = settings.getString("city_name", "Enter Location");
String a = settings.getString("city_address", "");
cityname.setText(n);
cetlocation.setText(a);
and i cleared data using this code in CleanlinessActivity
#Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences settings = getSharedPreferences("city_address", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("city_address");
editor.clear().commit();
}
By closing the app you mean just clicking the home button then onDestroy() is never called, you can get a refresher of the android life cycles here
If what you are doing is simply clicking the home button then consider moving your code to the onStop() otherwise you need to commit() following the remove(...) The android documentation states "Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called."
You have an instance of SharedPreferences called city_address which is having two fields or (Columns if we call it),but inside onDestroy()you are trying to to clear only one field of it called city_address,and the other field city_name field is left unchanged,if you want to completely remove the content of the city_address SharedPreferences use editor.clear().commit();
or``editor.clear().apply();`

Android activity refreshes flow

I have an activity A, it has a button to open B, which inserts a record. A can then refresh the view inside onActivityResult. This is a normal flow.
However, B can accept share intent to insert a record also. How can A know when this action is done from B to refresh the view just like the normal flow? (of course, I assume act A already running on background task)
I can, of course, detect the change using onResume inside A, but i wish to know if it is a proper method.
thank you
You can use onResume to refresh the view for activity A. Because other method would be using broadcastReceiver and broadcastIntent and that would create unnecessary load.
You can use sharedPreference to store if data has been changed in Activity B and then in onResume of Activity A fetch that shared Preference and refresh the view
#Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isChanged = sharedPreferences.getBoolean("isChanged",false);
if(isChanged){
//refresh your views
//clear the shared prefernce
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean("isChanged",false);
editor.apply();
}
}
Remember you have set that shared preference in ActivityB while loading data
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
editor.putBoolean("isChanged",true);
editor.apply();

How to display different content depending on whether the app is opened first time?

How to display different content depending on whether the app is opened first time or the user setup something?
Imagine a activity A, B, If the user open the app first time or haven't setup something in activity B, Then display activity B, otherwise display activity A.
How to overcome this? Thank you.
Use PreferenceManager. In activity A add check for the preference firstLaunch. If it's not set, launch activity B. Then, in activity B, set it.
It could look like:
Activity A
#Override
protected void onResume() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// By default assume "true", which means this is the first launch.
if (prefs.getBoolean("firstLaunch", true)) {
// Start Activity B and finish myself.
Intent i = new Intent(getApplicationContext(), ActivityB.class);
startActivity(i);
finish();
}
}
Activity B
// Do it after you've initialized everything, so Activity A can be launched:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this)
SharedPreferences.Editor editor = prefs.edit();
// Set "fistLaunch" to "false", so Activity A will start next time.
editor.putBoolean("firstLaunch", false);
editor.commit();
If you want to show different activity if user opened the app first time and different activity second or third time and so on then you can use below approach.
You can use the SharedPreference to achieve this.
Check the condition like this in the Splash Activity :
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
if(!preferences.getBoolean("isFirstRun", false)) {
// This mean App Launch First Time
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", true);
edit.commit();
// Open the B Activity
} else {
// This mean App Launch Second or third ... time and start the A Activity
}

Android: Shared Preference value doesn't change even after committing

In an Activity_A, i have:
public static final String PREFS_NAME = "MyPrefsFile";
SharedPreference settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("hasLoggedIn", true);
editor.commit();
in Activity_B i have:
//changing the previously added **city** value
SharedPreferences settings = getSharedPreferences(Activity_A.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("city", myCity);
editor.commit();
in Activity_C i have:
SharedPreferences settings = getSharedPreferences(Activity_A.PREFS_NAME, 0);
String city = settings.getString("city", "default");
//here i am getting the previous value of **city**, not the updated 1 from Activity_B
But once i restart the application then it gives the correct value.
What am i doing wrong?
Thank You
In Activity C where you want to show the value, when do you get the value from the SharedPreferences?
You should get the SharedPreferences values in the onResume method i think because if you do this in the onCreate method no changes will be there if you co back to Activity C.
This is because the onCreate method will only be called once the Activity is first created. When you navigate back (away) from Activity C it goes on the backstack and is later restored using the onRestart or onResume. This means that the onCreate method is not called again.
So i suggest that you do the getting from the SharedPreferences in the onResume method.
Activity lifecylce: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
I'm right?
Rolf

Categories

Resources