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();
Related
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);
}
I'm using RecyclerView to populate CardViews with my object "Income". Now I need one of the buttons in CardView to start new Activity and send that object to it.
Here's part of my button's onClickListener in Adapter:
holder.editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
editAt(income);
}
});
public void editAt(Income income){
Intent i = new Intent(context,IncomeAddActivity.class);
Bundle bundle = new Bundle();
// here I want to send that "income" object
i.putExtras(bundle);
startActivity(i);
}
Is there any easy method to do this, or my approach is totally wrong?
You need to make your class Income implements the interface Serializable. Then you can do this:
Bundle bundle = new Bundle();
bundle.putSerializable("object", income);
intent.putExtras(bundle);
startActivity(i);
You can also use Shared Preferences to store and get some data
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("view_mode", mCurViewMode);
ed.commit();
in first activity i have 3 button firs button go to subject list , second button show last seen activity
Im use this for last seen activity :
in subject list for each item save number in shared preferences and when user click on last seen number in subject list call and show last activity
this code work well but when i turn of phone or app not open for minutes last seen show nothing why?
please help me
in subject list class
shared = getSharedPreferences("count", MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
public static int counter;
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 0) {
Intent intent0 = new Intent(getApplicationContext(),Study.class);
startActivity(intent0);
counter=1;
SharedPreferences.Editor edit = shared.edit();
edit.putInt("count", counter);
edit.commit();
}
if (i == 1) {
counter=2;
SharedPreferences.Editor edit = shared.edit();
edit.putInt("count", counter);
edit.commit();
Intent intent1 = new Intent(getApplicationContext(),Mo.class);
startActivity(intent1);
}
if (i == 2) {
counter=3;
SharedPreferences.Editor edit = shared.edit();
edit.putInt("count", counter);
edit.commit();
Intent intent2 = new Intent(getApplicationContext(),Sa.class);
startActivity(intent2);
}
in fist activity that contain last seen button :
case R.id.last_seen_btn:
if (SubjectActivity.counter==0){
Toast.makeText(getApplicationContext(), " nothing",
Toast.LENGTH_LONG).show();
}
if (SubjectActivity.counter==1){
Intent intent = new Intent(getApplicationContext(),Study.class);
startActivity(intent);
}
if (SubjectActivity.counter==2){
Intent intent = new Intent(getApplicationContext(),Mo.class);
startActivity(intent);
}
if (SubjectActivity.counter==3){
Intent intent = new Intent(getApplicationContext(),Sa.class);
startActivity(intent);
}
You shall use always the "application preferences" and best is to access them via the Application object. There is caching on SharedPreferences aand it's creating some issues some time.
Also update always the SharedPreferences before opening the Activity (if i == 0)
public class MyApp extends Application {
private SharedPreferences mMyPref;
#Override
public void onCreate() {
super.onCreate();
mMyPref = getSharedPreferences("my_pref_name", MODE_PRIVATE);
}
public SharedPreferences getMySharedPreferences(){
return mMyPref;
}
}
In your parts of code where you want to read/write just use
((MyApp)getApplicationContext()).getMySharedPreferences() ....
of of course create a local variable that gives you the possibility to work with it.
I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data
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.