So basically I'm trying to pass an EditText String to another fragment which is in the same Activity. Then pass that data to another Activity, depending on which button the pressed. My problem is the application works fine but I just don't see the string being created. I've tried to use a textview just to check if it works when I pass it through the first data, but nothing shows.
I just want to pass the string to another fragment then depending on the button they press pass it on the whichever Activity.
This is my code
Passing my Data to the next Fragment and going to the fragment.
mNextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ItemStorageFragment.class);
intent.putExtra("itemName", addName.getText().toString());
((AddInventoryActivity)getActivity()).ToExpiration(null);
}
});
Grabbing the data from my First Fragment
Intent intent = getActivity().getIntent();
value = intent.getStringExtra("itemName");
Then Sending it to the Activity
mToFreezer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(getActivity(), FreezerActivity.class);
in.putExtra("itemNameToFreezer", value);
startActivity(in);
}
});
Then Adding it to my RecyclerView
Intent i = getIntent();
String string = i.getStringExtra("itemNameToFreezer");
mDataset.add(string);
mAdapter.notifyDataSetChanged();
Intent extras are fine to share data between Activities, but you cannot use them for Fragments directly, as you did in your code. I assume, you are a beginner, so I recommend you read in depth about Intents here: Android - Intents and Filters
There are several ways of passing data between Activities and Fragments, most common of which is passing arguments. Yet, in your situation I'd recommend using SharedPreferences. You can store String data at any point inside your Fragment or Activity and then easily take it out with these simple steps:
Input data into SharedPreferences:
SharedPreferences.Editor editor = getSharedPreferences("YourPrefsFile", MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();
Get data from SharedPreferences:
SharedPreferences prefs = getSharedPreferences("YourPrefsFile", MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "");
int idName = prefs.getInt("idName", 0);
}
Related
hi all i am new to android development my question is that how to call input edit text data in any activity where i required just like local storage in hmtl5 how to set value and get them where i required in any activity but not in the intent activity where i mentioned in the below example i want to show in some other activity means i need to get the values
here is my code
public class Autoinput extends Activity {
EditText Engines, Drivers;
Button next;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autoinputvalues);
Engines = (EditText) findViewById(R.id.noofengines);
Drivers = (EditText) findViewById(R.id.noofdrivers);
next = (Button) findViewById(R.id.autoinputnext);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent autoinputscreen = new Intent (getApplicationContext(), autocoverage.class);
startActivity(autoinputscreen);
}
});
}
}
if we write put extra in intent it will go to that particular activity which we mention intent and there only we can get it my intention is to call in any activity and show them.
You should use shared preference. You should refer this link
You can do this in two ways, one is using Bundle and the another one throught Intent.
Using Bundle,
To Send:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Key", "Value");
passIntent.putExtras(bundle);
startActivity(passIntent);
To Receive:
Bundle bundle = getIntent().getExtras();
String recText = bundle.getString("Key");
Using Through Intent:
To Send:
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", "Value");
startActivity(passIntent);
To Receive:
String recText = getIntent().getExtras().getString("Key");
For your Code, in your FirstActivity
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "clicked on item: " + position);
Intent passIntent = new Intent(CurrentActivity.this, SecondActivity.class);
passIntent.putExtras("Key", position);
startActivity(passIntent);
}
});
In SecondActivity,
details.setText(getIntent().getExtras().getString("Key"));
If you want the data to be destroyed after your application is closed or crashed you can use something like global variables: For example refer to this
On the other hand if you want your data to be persisted even after application crash, you can use shared preferences:
SharedPreferences pref;
pref=context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor=pref.edit();
Set data:
editor.putString("Engines", Engines.getText().toString());
editor.putString("Drivers", Drivers.getText().toString());
Then retrieve from anywhere:
String engines=pref.getString(("Engines",null);
String drivers=pref.getString(("Drivers",null);
if you want save your data and required in any activity must use Database or Share preference
To obtain shared preferences, use the following method In your activity:
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To read preferences:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Vboolean IsTurnOn = preferences.getBoolean("Key", Default_Value);
To edit and set preferences
Editor editor = preferences.edit();
editor.putBoolean("Key", Value);
editor.commit();
I've an application where a user creates events. The user need to retrieve a certain name from an activity which is a ListView of names list.
I'm having an issue with making sure that a name should remain in an activity after clicking a date button which links to another activity(calendar activity), then return back to the current activity.
My codes of the 3 pages:
Create_Events.java - codes for getting a certain name from ListView activity and the btnDate onClickListener which links to the another activity(calendar activity)
Bundle bundle = getIntent().getExtras();
if(bundle != null)
{
String date = bundle.getString("date");
txtDate.setText(date);
}
Bundle b = getIntent().getExtras();
if(b != null)
{
String name = bundle.getString("name");
txtName.setText("Create an event for:" +name);
}
buttonDate = (Button) findViewById(R.id.btnDate);
buttonDate.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent calIntent = new Intent(Create_Events.this, Calendar_Event.class);
startActivity(calIntent);
}
});
ContactsList.java -- the ListView of the names which is passed to the Create_Events page.
Cursor cursor = null;
cursor = (Cursor) l.getItemAtPosition(position);
Intent intent = new Intent(ContactsList.this, Create_Events.class);
intent.putExtra("name", cursor.getString(cursor.getColumnIndex(buddyDB.KEY_NAME)));
startActivity(intent);
I need help with this. Any help provided will be greatly appreciated. Thanks in advance! =)
you can get this behavior by saving you current screen state,
you can either use shared preferences or other ways (xml,data base, ..),
this way before you leave the activity (onPause) you save any information you need..
and on (onResume) if the information exists (its not the first time the activity loads),
collect the data and put it on screen..
if this is too much for you and you only need the name string to save,
try doing this :
How to declare global variables in Android?
hope it helps...
okay what i understand from your question is you want to retain your data on screen after coming back from another activity.
like Activity A--> Activity B--> Activity A
so, set in menifest file for activity A
android:launchmode="singletop"
and, when you are coming back from Activity B to Activity A
set
Intent intent=new Intent(ActivtyB.this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
you can use SharedPreferences to store the name while use bundle to store the date.
From contactLists.java add these codes
private void SavePreferences(String key, String value)
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(key, value);
editor.commit();
}
private void LoadPreferences()
{
SharedPreferences sharedPref = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
String name = sharedPref.getString("name", "");
}
Then set the name to the textView which will show on the listView. And then load the preference in the create_events page and the name will be shown even when you go to another activity.
Do inform me if you still have any questions. (:
i am new to Android development. I have two activities A and B. In activity A I use a xml parser to gain objects with each about 10 strings included. I want to pass these objects to activity B and there should be a listview showing all the objects. Clicking on a object in the listview should show the 10 strings.
I am not sure if I have to use a SQLite database or if I only can use SharedPreferences?
Or can I even store it on the internal memory?
The objects should be saved even if i kill the app.
Hope someone can give me some hints, thanks!
this may helps you
you can pass your string data both way as you like [1] Using Intent [2] Using Share Prefrence
like [1] Intent
in your First ActivityA
Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
myintent.putExtra("Name1", "your String");
myintent.putExtra("Name2", "your String");
myintent.putExtra("Name3", "your String");
myintent.putExtra("Name4", "your String");
startActivity(myintent);
in Second ActivityB
Intent myintent = getIntent();
if(null!=myintent.getExtras()){
String Name1 = myintent.getExtras().getString("Name1");
String Name2 = myintent.getExtras().getString("Name2");
String Name3 = myintent.getExtras().getString("Name3");
String Name4 = myintent.getExtras().getString("Name4");
Toast.makeText(getApplicationContext(),""+Name,12).show();
}
else
{
Toast.makeText(getApplicationContext(),"No Recor Here..",12).show();
}
like SharedPreferences[2]
in your First ActivityA
Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
SharedPreferences spref = this.getSharedPreferences("mynotifyid", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor spreedit = spref.edit();
spreedit.putString("Name1", str1.toString());
spreedit.putString("Name2", str2.toString());
spreedit.putString("Name3", str3.toString());
spreedit.putString("Name4", str4.toString());
spreedit.commit();
startActivity(myintent);
in your Second ActivityB
SharedPreferences spref = context.getSharedPreferences("mynotifyid", Context.MODE_WORLD_WRITEABLE);
String str1 = spref.getString("Name1","");
String str2 = spref.getString("Name2","");
String str3 = spref.getString("Name3","");
String str4 = spref.getString("Name4","");
for your object saving purpose use SharedPreferences
Let these objects implement the interface Serializable so you can pass the objects to another Activity using an Intent
Maybe this small(!) example can help you:
public class MyModel implements Serializable {
...
}
public A extends Activity {
public void onCreate(Bundle savedInstanceBundle) {
...
//fetchData
...
MyModel data = new MyModel(fetchedData);
Intent intent = new Intent(this, B.class);
intent.putExtra("KEY", data);
startActivity(intent);
}
}
public B extends Activity {
public void onCreate(Bundle savedInstanceBundle) {
Bundle extras = getIntent().getExtas();
MyModel data = (MyModel) extras.getSerializable("KEY");
...
//handle data
...
}
}
For those 10 strings I wouldn't use database.
SharedPreferences easy to use, lot of examples and will deal with your needs.
If a none string need later, than you can use internal memory ( binary mode ), but now no reason, imho.
i want to get text written in the EditText first activity and set that text to the another EditText which is fourth activity.
Use this code in your first activity
Intent intent = new Intent(context,Viewnotification.class);
intent.putExtra("Value1", youredittextvalue.getText().toString());
startActiviy(intent);
And in your fourth Activity
Bundle extras = getIntent().getExtras();
String value1 = extras.getString("Value1");
yourfourthactivityedittext.setText(value1);
1- use SharedPreferences
2- set in apllication class
3- pass to using intent from 1-> 2 ->3 ->4
Simple way to do is,
You can assign one static variable which is public inside first activity like,
public static String myEditTextContent;
Set this after you set the value from your edit text like,
myEditTextContent = editText.getText().toString();
Use the same in fourth activity like
FirstActivityClass.myEditTextContent and set it in this(fourth) activity.
Later on you can use intent's putExtra,SQLLite Database,Shared Preference also, as suggested by others
You can do it in two ways
First Use SharedPreferences like
// declare
SharedPreferences pref;
SharedPreferences.Editor edit;
in On create
//initialize
pref = this.getSharedPreferences("myPrefs", MODE_PRIVATE);
edit = pref.edit();
// add data in it
edit.putString("USERNAME", EditText1.getText().toString());
edit.putString("PASSWORD", EditText1.getText().toString());
edit.putString("USERID", Text1.getText().toString());
// save data in it
edit.commit();
to get data
// access it
String passwrd = pref.getString("PASSWORD", "");
String userid = pref.getString("USERID", "");
And the second way
Send data from 1 to 2 and 2 to 3 and 3 to 4 activity
with intents like
Intent i = new Intent(First.this, Secondclass.class);
i.putExtra("userid", EditText1.getText().toString());
i.putExtra("username",EditText2.getText().toString());
startActivity(i);
and recieve in each activity like
Intent i = getIntent();
String ursid = i.getStringExtra("userid");
String ursername = i.getStringExtra("username");
i am new developer in android applications.i would like to save the data using shared preference concept.i am saving the data in one activity and get the same data in another activity.here i would like to send the String a[]={"one","two","three"} one activity to another activity.i have written code as follows
Main1.java
public class Main1 extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences shp=getSharedPreferences("TEXT", 0);
final Editor et=shp.edit();
((Button)findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String s1=((EditText)findViewById(R.id.editText1)).getText().toString();
et.putString("DATA", s1);
String s2[]={"one","two","three"};
//here i would like to save the string array
et.commit();
Intent it=new Intent(Main1.this,Main2.class);
startActivity(it);
}
});
}
Main2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
String kk=getSharedPreferences("TEXT", 0).getString("DATA", null);
//here i would like to get the string array of Main1.java
((EditText)findViewById(R.id.editText1)).setText(kk);
}
can we get the string array values from Main1.java to Main2.java?
Put it into the starting intent:
Intent it = new Intent(Main1.this,Main2.class);
it.putExtra("MY_STRING_ARRAY", s2);
Get it back in the second activity:
String[] myStringArray = getIntent().getStringArrayExtra("MY_STRING_ARRAY");
If you want to send data from one activity to another then the best way would be send data using Intent object's putExtra method
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("data1", "some data");
i.putExtra("data2", "another data");
i.putExtra("data3", "more data");
startActivity(i);
and you can get the data from receiving activity Activity2 like this
Object data1 = getIntent().getExtras().get("data1");
Hope that helps
If you want to save your information via SharedPreference not just pass it along activities, use some code like this:
SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settings.edit();
prefEditor.putString("string_preference", "some_string");
prefEditor.putInt("int_preference", 18);
prefEditor.commit();
The commit command is the responsable of actually saving data to SharedPreferences.