Favorites - How would I implement a favorites button so when it is pressed, the textView text is saved to an activity called favorites?

StackOverflow https://stackoverflow.com/questions/15774079

  •  31-03-2022
  •  | 
  •  

سؤال

I am creating an app with textView boxes. When a button is pressed the text changes using a random generator. How do I implement a favorite button so when it is pressed, a toast is created and the textView text is saved to an activity called favorites?

هل كانت مفيدة؟

المحلول

I would save the "textView" content as a SharedPreference in the onClick() method of the Button.

When the user accesses the Favorites Activity, I would send the SharedPreference value to the other Activity via the Extras Bundle

Depending on where you have to manage the list of stored Favorite values and how many there are, you may have to use a database instead of SharedPreference.

نصائح أخرى

For this what are intents made for...

say you want to send data from Activity A to B

1>then in A's oncreate() call this

Intent i = new Intent(this,your.class);
i.putExtra("key", value);
startActivity(i);

2>and in B's oncreate() use this

int value = getIntent().getExtras().getInt("key");

i guess you got what i meant...give it a try...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top