How to call strings in fragment from activity? - android

I have Activity A that has String that updates every 1s.
I also have Fragment B which has a Button and a TextView.
When I press my button in Fragment B I want to get String from Activity A to my TextView.
How do I do this?

SharedPreferences.Editor editor = getSharedPreferences(YOUR_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("DATA_NAME", "YOUR_DATA");
editor.commit();
On the Button Click
SharedPreferences prefs = getSharedPreferences(YOUR_PREFS_NAME, MODE_PRIVATE);
String restoredData = prefs.getString("DATA_NAME", DEFAULT);
yourTextView.setText(restoredData );

Related

How to save viewpager page position as bookmark?

I have MainActivity where i define viewpager and i also define button on toolbar.
I want when i click on button it save the viewpager page position with title in another activity in listview.
Provide complete example.
Try saving position of viepager in a shared preference.
save when going away from activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("viepager_position", postion);
editor.apply();
retrieve when user comes back to activity
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
int position = prefs.getInt("viepager_position",default_position);

How to return data from a fragment using androidx.navigation?

I open a selection fragment using androidx.navigation, but I cannot find a way to return data to the previous fragment.
In one parent activity.. just use global variable that able to access inside same parent activity or
Other trick you can try preference, so that preference will able to read anywhere you want.
Data share active fragment
sharedPref = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString(sKey, sValue);
prefEditor.apply();
Data read from other fragment
sharedPref = context.getSharedPreferences(context.getPackageName(),MODE_PRIVATE);
sValue = sharedPref.getString(sKey,defValue);

how to make check box check on button click in different activity in android

in my android app there is terms and condition fragment and in that there are two buttons accept and decline, the scenario is when user click on terms and condition the fragment open and user have two options accept and declined and when he press the accept button it return to main activity and there is one checkbox that should be check when user click on accept button in the fragment,So can anyone tell me how should i do that?
By using SharedPreferences short quick example:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
editor.putBoolean("switchOn", true);
editor.commit();
on the second activity
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
Editor editor = pref.edit();
boolean buttonOn = editor.getBoolean("switchOn", false);

How to get a preference value in preference fragment

I want to show the value of preferences in a custom preference screen. For this I need to get the values, in this case strings, within the preference fragment. In non fragments (activities) I get the values with e.g. final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); and get the string with String phonenumber = prefs.getString("preference_name", null); but in the preference fragment getDefaultSharedPreferences is not applicable for preference fragment.
Any idea how to solve this?
Here's my code snippet:
public class PreferencesFragment extends PreferenceFragment implements
OnSharedPreferenceChangeListener {
TextView tvusername, tvphonenumber;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
// entering preference phonenumber in text view
String phonenumber = prefs.getString("phonenumber", null);
;
tvphonenumber.setText(phonenumber);
// entering preference username in text view
String username = prefs.getString("username", null);
;
tvusername.setText(username);
}
In onActivityCreated (It's the time when the activity is created) of your fragment class, you do
Context hostActivity = getActivity();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(hostActivity);
That's how you access the hostActivity from the attached fragment.

How to link a favorite button to a new activity?

I want to add a favorite button in a layout which can save the layout and apply it to another activity. I'll show you an image bellow to understand exactly the way I want to do it..
I created the favorite button, and the activity for this button to save the layout, but the app is still crashing when I press the favorite button and the activity which should open the layout, which I have saved, is blank. Here are the code:
public void onClick(View v){
SharedPreferences prefs = this.getSharedPreferences(
getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
prefs.edit().putString("activity", "YourActivityName");
}
public void onClick(View v){
SharedPreferences prefs = this.getSharedPreferences(
getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
String activityToRun = prefs.getString("activity", "noFavorites");
if(activityToRun.equals("noFavorites")
return null;
if(activityToRun.equals("YOUR FIRST ACTIVTY")
//code for launching activity
}
I added this to Favorite.java, and I created another activity, called Favorite2.java which should be open the layout saved by the favorite button, but what I don't understand is how I can link the saved layout to the activity Favorite2.java?
And please, if someone can tell me exactly the explanation for getString and putString and the way that they should be implemented.

Categories

Resources