I am new to android!.
I have made spinner in activity 1 (one time open Activity) and want to transfer selected value from spinner by user to activity 2.I have used sharedPreference for one time open activity .Its only working 1st time but second time it cant transfer data.I am stuck here please help me
SharedPreferences Preferences=getSharedPreferences("PREFERENCE",MODE_PRIVATE);
String FirstTime=Preferences.getString("FirstTimeInstall","");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String parent=sp_parent.getSelectedItem().toString();
String child=sp_child.getSelectedItem().toString();
String college=sp_college.getSelectedItem().toString();
Intent intent=new Intent(getApplicationContext(),Dashboard.class);
intent.putExtra("parent",parent);
intent.putExtra("child",child);
intent.putExtra("college",college);
startActivity(intent);
finish();
}
});
if (FirstTime.equals("Yes")){
String parent=sp_parent.getSelectedItem().toString();
String child=sp_child.getSelectedItem().toString();
String college=sp_college.getSelectedItem().toString();
Intent intent=new Intent(getApplicationContext(),Dashboard.class);
intent.putExtra("parent",parent);
intent.putExtra("child",child);
intent.putExtra("college",college);
startActivity(intent);
finish();
}else {
SharedPreferences.Editor editor=Preferences.edit();
editor.putString("FirstTimeInstall","Yes");
editor.apply();
}
When starting a new activity you can send data through intent.
Intent intent = new Intent( this, Activity2.class );
intent.putExtra( "key", "value" );
startActivity(intent)
And in second activity do like this:
getIntent().getStringExtra( "key" );
or if you are sending int for example
getIntent().getIntExtra( "key", 0 );
Related
I have Activity1, Activity1 Adapter and Activity2
I'm not able to pass value between an Adapter and Activity. When back button is pressed, I'm expecting a value to be coming from the Second Activity (Activity 2) to Activity1 Currently, it gives me null
Here are my code snippets.
Activity1 Adapter
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Activity origin = (Activity) context;
Intent intent = new Intent(context, PostActivity.class);
intent.putExtra("searchText", staggeredCustomCard.getSearchText());
origin.startActivityForResult(intent, 1);
}
});
Activity2
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
mIntent.getStringExtra("searchText");
setResult(1, mIntent);
}
I'm expecting this searchText to be going to Activity1. Could anyone please guide me how to achieve this?
You are getting getStringExtra("searchText"); from a completely new intent, that's why it's returning null. You need to get search text from getIntent() like this:
#Override
public void onBackPressed() {
super.onBackPressed();
Intent mIntent = new Intent();
String search = getIntent().getStringExtra("searchText");
mIntent.putExtra("searchText", search);
setResult(1, mIntent);
}
I have 2 EditTexts. EditText1 is in MainActivity, EditText2 is in SecondActivity.
EditText1 is to login (password), EditText2 is to change password.
My code looks like this:
EditText editText1 = findViewById(R.id.login);
editText2 = findViewById(R.id.changePassword); // declared in SecondActivity
if (editText1.getText().toString().equals(editText2.getText().toString())
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "Password incorrect", Toast.Length_Long).show;
}
When I press Button to login, it shows me an error. I know it has to be initialized in a different way but how?
I tried another code with a Dialog and everything worked perfectly:
changePasswordDialog = new Dialog(MainActivity.this);
changePasswordDialog.setContentView(R.layout.activity_second_activity);
editText2 = changePasswordDialog.findViewById(R.id.changePassword);
So it works perfectly with Dialog, but how does it work without Dialog?
Try Like this
Store edit text 2 pass
String pass = editText2.getText().toString().trim();
SharedPreferences.Editor editor = getSharedPreferences(My_Prefs,Context.MODE_PRIVATE).edit();
editor.putString("pass", pass);
editor.apply();
Now Retrive the stored passwod in MainActivity
SharedPrefrences prefrences = getSharedPrefrences(My_Prefs,Context.MODE_PRIVATE);
String pass = prefrences.getString("pass","");
if (editText1.getText().toString().equals(pass)
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
You can use intent put extra and intent get extra for this
/// you can use this as per your requirement or you can use sharedprefrence///
// In main activity ///
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
intent.putExtra("editText1",editText1.getText().toString());
startActivity(intent);
/// In second activity//
String passWord = getIntent().getExtras().getString("editText1");
Log.d("password : ",passWord);
I want to transfer my "result" data from my first (Main) acitivity to
my Custaddress activity, which has edit texts for customer details, and then this is sent to an email. The email/edit texts work perfectly - but I want to
add in "result.toString" into email body string. How do I transfer "result" to the second activity? I believe its something to do with arg?
Here's my code from first activity..
DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
result.append("\nTotal: £"+decimalFormat.format(totalamount));
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setMessage(result.toString());
alertDialogBuilder.setTitle("YOUR ORDER");
alertDialogBuilder.setPositiveButton("Accept",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
//do what you want to do if user clicks ok
//Intent intent = new Intent(context, Custaddress.class);
// startActivity(intent);
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
}
});
alertDialogBuilder.setNegativeButton("Decline",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do what you want to do if user clicks cancel.
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
Write it in activity that passing data
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("key",value);
startActivity(custaddress);
Write below code in activity that catching data
Intent intent=getIntent();
String mString=intent.getStringExtra("key");
hope this will help you
You should use intent (click here) :
Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);
intent.putExtra("text", mytext);
startActivity(intent);
You just need to replace your lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
startActivity(custaddress);
with these three lines:
Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
custaddress.putExtra("result", result.toString());
startActivity(custaddress);
and then, when you open the new activity (in your case the Custaddress Activity), you should do the following to get your result
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("result");
}
You should add Extra in the Intent object which you are passing in the startActivity(intent) method.
Example
String value = "String i want to send to next activity"
Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);
intent.putExtra("KEY", value);
startActivity(intent);
In the Custaddress.java Activity class you need to get the data from the Bundle object that you get as a parameter in the onCreate(Bundle bundle) method
Bundle extras = getIntent().getExtras();
if (extras != null) {
// get data via the key
String valueFromPreviousActivity = extras.getString("KEY");
if(valueFromPreviousActivity != null){
// do something with the data
}
}
Check out this official doc Starting Another Activity.
Through the below code we can send the values between activities
use the below code in parent activity
Intent myintent=new Intent(Info.this, GraphDiag.class).putExtra("<StringName>", value);
startActivity(myintent);
use the below code in child activity
String s= getIntent().getStringExtra(<StringName>);
so what im trying to do is take the int "inc" and .putPutExtra("Inc",inc) it to my second class called "Settings" and then save it via "SharedPreferences" and then when the user pushes the button to go back to my main class it auto loads that into a int
heres the code i used to send it to the settings class from MainActivity
int inc = 0;
Intent GoToSettings = new Intent(getApplicationContext(), settings.class);
GoToSettings.putExtra("Inc", inc);
startActivity(GoToSettings)
heres the code that receves it
Intent intent = getIntent();
Inc = intent.getIntExtra("Inc", 10);
heres the code that saves "Inc"
SharedPreferences Saveinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
SharedPreferences.Editor SaveIncE=Saveinc.edit();
SaveIncE.putInt("IncSave", Inc);
SaveIncE.commit();
heres the code that receves the int after its saved
int inc = 1;
Intent GoToMain = getIntent();
inc = GoToMain.getIntExtra("IncSave", 1);
SharedPreferences GetSavedinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
int IncSave=GetSavedUpCount.getInt("IncSave", 1);
inc=IncSave;
i no its not the prittyest layout but im new and havent really learned the syntex perfictly.
im also new to the website to so if i dissobay a rule im sorry.
In your last activity where you just want value from SharedPreference in that just use this.
//int inc = 1;
//Intent GoToMain = getIntent(); //Comment all this..
//inc = GoToMain.getIntExtra("IncSave", 1);
SharedPreferences GetSavedinc=getSharedPreferences("IncSave", Context.MODE_PRIVATE);
int IncSave=GetSavedUpCount.getInt("IncSave", 1);
Log.v("My Value",IncSave+"");
if your int 'inc' is going to end up in your SharedPreferences in the end no matter what, you could just skip passing it to the other class and just write it into your prefs in your main activity.
You can just read it from shared prefs during the onCreate/onResume when you return to your main activity, and you can read it from your shared prefs in the constructor of your settings class.
If you want to send some data between 2 activities, you should never use shared pref.
Below is prefered way to do this :
Send data from Activity1 to Activity2 :
Intent intent = new Intent();
intent.putExtra("numberFromActivity1", 122);
startActivityForResult(intent, REQUEST_CODE);
In Activity2, read this using below code :
Intent intent = getIntent();
int inc = intent.getIntExtra("numberFromActivity1", 0); // 122
Send data from Activity2 to Activity1 : You can set result in onDestroy() or in onBackpressed() methods.
Intent intent = new Intent();
intent.putExtra("numberFromActivity2", 221);
setResult(RESULT_OK, intent);
finish(); // Finish second activity
Now in Activity1, you can get this data in onActivityResult() method :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE) {
int numberFromActivity2 = data.getIntExtra("numberFromActivity2" , 0); // 221
}
}
i'm having an issue with Intents and putExtra.
What i want to do is this :
In Activity A(it's not my MainActivity),when i click a button,it will close all my activities, send a string and launch my main activity.For testing purposes it will show a test dialog with my string.All good till now,works as i need it to.
The problem is that if i restart my MainActivity(and i need to do that,it's something like a shopping list,i need to start a new shopping list) the dialog with the putExtra string shows again.
Here are my code snippets :
In Activity A :
#Override
public void onClick(View v) {
Intent intent = new Intent(Gestionarez.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra( "paramName", str );
startActivity( intent );
// TODO Auto-generated method stub
dialog.dismiss();
dialog.cancel();
}
In my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadPref();
Bundle extras = getIntent().getExtras();
if (extras != null) {
String myParam = extras.getString("paramName");
ShowAlertMessage(this, "TEST", myParam + "");
} else {
}
}
And this is how i restart my MainActivity when i need to start a new shopping list :
Intent intent = getIntent();
finish();
startActivity(intent);
replace
Intent intent = getIntent();
finish();
startActivity(intent);
with
Intent intent = new Intent(this, ActivityB.class);
finish();
startActivity(intent);