RadioButton value duplicate with each other from different activities - java

I have this function that call RadioButton value from the Group.java to the Add.java. I use the same function on another activity called Status.java. Now, every time I click from either Group.java or Status.java, the result become duplicate. And every time I click the RadioButton, my EditText will dissappear.
Group.java
RadioGroup radiog1;
RadioButton radio1, radio2, radio3, radio4, radio5;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_group);
radiog1 = (RadioGroup) findViewById(R.id.radiog1);
radio1 = (RadioButton) findViewById(R.id.radio1);
radio2 = (RadioButton) findViewById(R.id.radio2);
radio3 = (RadioButton) findViewById(R.id.radio3);
radio4 = (RadioButton) findViewById(R.id.radio4);
radio5 = (RadioButton) findViewById(R.id.radio5);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
radio1.setOnClickListener(this);
radio2.setOnClickListener(this);
radio3.setOnClickListener(this);
radio4.setOnClickListener(this);
radio5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.radio1):
returnIntent.putExtra("GroupTag", "" + radio1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio2):
returnIntent.putExtra("GroupTag","" + radio2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio3):
returnIntent.putExtra("GroupTag", "" + radio3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio4):
returnIntent.putExtra("GroupTag", "" + radio4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.radio5):
returnIntent.putExtra("GroupTag","" + radio5.getText());
setResult(RESULT_OK,returnIntent);
finish();
break;
}
}}
Status.java
RadioButton rb1, rb2, rb3, rb4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .8), (int) (height * .6));
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb4 = (RadioButton) findViewById(R.id.rb4);
rb1.setOnClickListener(this);
rb2.setOnClickListener(this);
rb3.setOnClickListener(this);
rb4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent returnIntent = getIntent();
switch (v.getId()) {
case (R.id.rb1):
returnIntent.putExtra("StatusTag", "" + rb1.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb2):
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb3):
returnIntent.putExtra("StatusTag", "" + rb3.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
case (R.id.rb4):
returnIntent.putExtra("StatusTag","" + rb4.getText());
setResult(RESULT_OK, returnIntent);
finish();
break;
}
}}
Add.java
ImageButton ibtn, ibtn2, ibtn3, ibtn4,ibtn5;
TextView tvgroup;
TextView tvstatus;
int groupRequestCode;
int statusRequestCode;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
tvgroup = (TextView) findViewById(R.id.tvgroup);
tvstatus = (TextView) findViewById(R.id.tvstatus);
ibtn = (ImageButton) findViewById(R.id.ibtn);
ibtn2 = (ImageButton) findViewById(R.id.ibtn2);
ibtn3 = (ImageButton) findViewById(R.id.ibtn3);
ibtn4 = (ImageButton) findViewById(R.id.ibtn4);
ibtn5 = (ImageButton) findViewById(R.id.ibtn5);
ibtn.setOnClickListener(this);
ibtn2.setOnClickListener(this);
ibtn3.setOnClickListener(this);
ibtn4.setOnClickListener(this);
ibtn5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.ibtn):
startActivityForResult(new Intent(this, Group.class), groupRequestCode);
break;
case (R.id.ibtn2):
startActivity(new Intent(this, Due_Date.class));
break;
case (R.id.ibtn3):
startActivity(new Intent(this,DueTime.class));
break;
case (R.id.ibtn4):
startActivityForResult(new Intent(this, Status.class), statusRequestCode);
break;
case (R.id.ibtn5):
startActivity(new Intent(this,Assignees.class));
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}}
The result :
Any kind of help would really be appreciated.

I suggest you to use startActivityForResult() method to pass data between activities.
So you need to change your code like that:
In Add.java
1) Remove this code:
Bundle extra = getIntent().getExtras();
if (extra != null) {
String textViewName = extra.getString("SomeTag");
tvgroup.setText(textViewName);
}
Bundle extra2 = getIntent().getExtras();
if (extra2 != null) {
String status = extra2.getString("SomeTag");
tvstatus.setText(status);
}
2)
Change startActivity(new Intent(this,Group.class));
to startActivityForResult(new Intent(this,Group.class), groupRequestCode);
Also startActivity(new Intent(this,Status.class));
to startActivityForResult(new Intent(this,Status.class), statusRequestCode);
PS: groupRequestCode should be different from statusRequestCode ( for example 1 and 2).
3) Overrid onAcitivtyResult() method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == groupRequestCode) { // here you come back from Group.java
if(resultCode == RESULT_OK){
// do your stuff here
String textViewName = data.getStringExtra("GroupTag");
tvgroup.setText(textViewName);
}
}
if (requestCode == statusRequestCode) { // here you come back from Status.java
if(resultCode == RESULT_OK){
// do your stuff here
String status = data.getStringExtra("StatusTag");
tvstatus.setText(status);
}
}
}
PS: you can use switch instead of if blocs if you have many requestcode.
In Group.java
Change
Intent intent = new Intent(Group.this, Add.class);
intent.putExtra("SomeTag", "" + radio1.getText());
startActivity(intent);
to
Intent returnIntent = getIntent();
returnIntent.putExtra("GroupTag","" + radio1.getText());
setResult(RESULT_OK,returnIntent);
finish();
=> Do the same thing for the rest of the RadioButtons
In Status.java
Change
Intent intent2 = new Intent(Status.this, Add.class);
intent2.putExtra("SomeTag", "" + rb2.getText());
startActivity(intent2);
to
Intent returnIntent = getIntent();
returnIntent.putExtra("StatusTag","" + rb2.getText());
setResult(RESULT_OK, returnIntent);
finish();
=> Do the same thing for the rest of the RadioButtons

Related

How to store button information from previous activity, Android Studio Edition

So, I have an activity with 2 buttons marsPhoto and viewByDate. After the buttons are clicked they go to a new activity page where I want to take the name from the putExtra (Daily or Date) and printout Daily or Date in the TextView. I only want to set the text to match the string for the button (which is Daily for intent2 clicked and Date for intent3 clicked).
Activity 1
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("Daily",1);
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("Date",2);
startActivity(intent3);
break;
}
}
Activity 2
TextView type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
Intent i = getIntent();
String s1 = i.getStringExtra("Daily");
String s2 = i.getStringExtra("Date");
type.setText(s1);
}
Change the key for the passed StringExtra to something like "button_text" and set the value to the text you want to display, i.e. "Date" or "Daily". There doesn't seem to be any meaning to the integer you are transferring.
modify the first code to this
Activity 1
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.marsPhoto:
Intent intent2 = new Intent(this, MARS.class);
intent2.putExtra("dataSent","Daily");
startActivity(intent2);
break;
case R.id.viewByDate:
Intent intent3 = new Intent(this, MARS.class);
intent3.putExtra("dataSent","Date");
startActivity(intent3);
break;
}
}
Activity 2
TextView type;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mars);
type = (TextView)findViewById(R.id.type);
String s1="";
Bundle extras = getIntent().getExtras();
if(extras == null) {
s1 = "";
} else {
s1 = extras.getString("dataSent","");
}
type.setText(s1);
}

Stuck trying to link up pages

I got the first-page working but got a youtube tutorial for the second but can't seem to get it working the way I want it to.the pages are done differently.
This is the working page
private void setSingleEvent(GridLayout mainGrid) {
//Loop all child item of Main Grid
for (int i = 0; i < mainGrid.getChildCount(); i++) {
//You can see , all child item is CardView , so we just cast object to CardView
CardView cardView = (CardView) mainGrid.getChildAt(i);
final int finalI = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finalI == 0) // open page 1
{
Intent intent = new Intent (MainActivity.this, StarterVapes.class);
startActivity(intent);
}
else if (finalI == 1) // open page 2
{
Intent intent = new Intent (MainActivity.this, UpgradeVapes.class);
startActivity(intent);
}
This is the nonworking page
#Override
public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
viewHolder.imgView.setImageResource(imagesList.get(i));
viewHolder.txtTitle.setText(titleList.get(i));
final int finalI = i;
viewHolder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (finalI == 0) // open page 1
{
Intent intent = new Intent (context, AboutUS.class);
context.startActivity(intent);
}
else if (finalI == 1) // open page 2
{
Intent intent = new Intent (context.this, AboutUS.class);
context.startActivity(intent);
}
// Intent intent = new Intent(context, DetailActivity.class);
// intent.putExtra("title", titleList.get(i));
// context.startActivity(intent);
}
});
I want it to were you click on the first tile it goes to a page, you click on the second tile it goes to a different page and so on.
Got it working
Heading
````
if (finalI == 0) // open page 1
{
Intent intent = new Intent (context, AboutUS.class);
intent.putExtra("title", titleList.get(i));
context.startActivity(intent);
}
````

Alertdialog doesn't show when within if statement

I'm building a small app that scans barcodes and put them in a database (using ZXing library). After the scan is taken, and the barcode doesn't exist in the database I want to show a popup dialog that allows an user to enter the name manually.
The dialog works perfectly if it's invoked in an onClickListener, however when I try to put it in an 'if' statement within another method (which checks if the scan result is in the database) the dialog doesn't show.
What's strange, I put tags in the beginning of the method and at the end, both of them are shown in the log, but the alertdialog doesn't appear and it gets me back to the main activity without any error...
public void popUP (String barcode) {
Log.d(TAG, "method started");
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_input, null);
TextView tv = (TextView) mView.findViewById(R.id.textView2);
final EditText et = (EditText) mView.findViewById(R.id.item1);
Button bt1 = (Button) mView.findViewById(R.id.button2);
Button bt2 = (Button) mView.findViewById(R.id.button3);
final String code = barcode1;
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mydb.addNewItem(et.getText().toString(), code);
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
}
});
mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
Log.d(TAG,"dialog shown");
}
And here is the method from which I try to inflate the popUP
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
final String barcode = scanResult.getContents();
if (scanResult != null) {
if(scanResult.getContents() == null) {
Log.d(TAG, "Cancelled Scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d(TAG, "SCANNED");
int rows = mydb.checkRows(barcode);
if (rows == -1){
Log.d(TAG, "popUP should appear");
popUP(barcode); //INVOKING THE ALERT DIALOG
} else {
Log.d(TAG, "found and attempting adding");
mydb.addItem(barcode, rows);
}
}
} else {
Toast.makeText(this, "oddal zero", Toast.LENGTH_LONG);
}
Intent intynt = new Intent(this, MainActivity.class);
startActivity(intynt);
}

How to check which button was pressed in startActivityForResult, override onActivityResult method?

I have three button (R.id.1, R.id.2, R.id.3) and three textView(a,b,c). How can I check which button was pressed onActivityResult so that the TextView can be setText accordingly to the button?
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
Example: In case 0, R.id.1 was pressed, so will be a.setText(b)....
If R.id.1 was pressed, a.setText()
If R.id.2 was pressed, b.setText()
If R.id.3 was pressed, c.setText()
Code
Button button1 = (Button) claims.findViewById(R.id.1);
Button button2 = (Button) claims.findViewById(R.id.2);
Button button3 = (Button)claims.findViewById(R.id.3);
a=(TextView)claims.findViewById(R.id.textView1);
b=(TextView)claims.findViewById(R.id.textView2);
c=(TextView)claims.findViewById(R.id.textView3);
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, 1);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, 2);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
}
Edited
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, requestCode);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivityForResult(intent, requestCode);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivityForResult(intent, requestCode);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivityForResult(intent, requestCode);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivityForResult(intent, requestCode);
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Toast.makeText(getActivity(), "Not coeted ", Toast.LENGTH_LONG).show();
switch (requestCode) {
case requestCode1:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
c.setText(" " + b + "------" + "RM " + result);
Toast.makeText(getActivity(), "Not completed ", Toast.LENGTH_LONG).show();
break;
case requestCode2:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
c.setText(" " + b1 + "------" + "RM " + result1);
break;
case requestCode3:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
c.setText(" " + b2 + "------" + "RM " + result2);
break;
}
}
int requestCode1 = 1;
int requestCode2 = 2;
int requestCode3 = 3;
Button b1 =(Button) findViewById(R.id.button1);
Button b2 =(Button) findViewById(R.id.button2);
Button b3 =(Button) findViewById(R.id.button3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialogRadio(requestCode1)
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialogRadio(requestCode2)
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialogRadio(requestCode3)
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == requestCode1){}//button 1
else if(resultCode == requestCode2){}//button 2
else if(resultCode == requestCode3){}//button 3
}
public void AlertDialogRadio(final int requestCode) {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, requestCode);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, requestCode);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, requestCode);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
you can check with Request Code.
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio(v.getId());
}
};
public void AlertDialogRadio(int id) {
final CharSequence[] ClaimsModel = {"Sunny", "Raining", "Cloudy"};
switch(id){
case R.id.1:
break;
case R.id.2:
break;
case R.id.3:
break;
}
Bundle bundle = new Bundle();
bundle.putString("key", "value");
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select Weather");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Sunny.class);
startActivityForResult(intent, 0, bundle);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Rainy.class);
startActivityForResult(intent, 1, bundle);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), Cloudy.class);
startActivityForResult(intent, 2, bundle);
}
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle bundle = data.getExtras();
// this can any data that you passed
String anyData = bundle.getString();
switch(requestCode)
{
case 0:
//check which button was pressed
String b = data.getStringExtra("a");
//Apply setText(b);
}
break;
case 1:
//check which button was pressed
String result1=data.getStringExtra("text");
// Apply setText(result);
break;
case 2:
// check which button was pressed
String b2 = data.getStringExtra("a");
// Apply setText (b2);
break;
}

How to receive multiple values using an intent

I have my main activity using the startActivityForResult method which calls an activity that i need to return two string values from. I have it working to return one, but even with all the tutorials and other questions on here i have read i cant seem to get it to return the two values. Below is my code.
here is where i start the second activity:
Button button = (Button) findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(addM, 1);
}
});
Here is the activity it starts, i need to return the text that is in the titleField(which works now) and the yearField
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.setData(Uri.parse(titleField.getText().toString()));
setResult(RESULT_OK, data);
//data.setData(Uri.parse(yearField.getText().toString()));
//setResult(RESULT_OK, data);
finish();
}
});
}
}
Here is the method in my main class that receives results
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getData().toString();
//tempYear = data.getStringExtra("movieYear");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
The code that is commented out was one attempt at making it receive multiple values, although they failed. Any help with this situation would be great. Thanks!
You should use something like:
Intent returnIntent = new Intent();
returnIntent.putExtra("title",titleField.getText().toString());
returnIntent.putExtra("year",yearField.getText().toString());
setResult(RESULT_OK,returnIntent);
finish();
And on your main activity, onActivityResult:
tempTitle = data.getStringExtra("title");
tempYear = data.getStringExtra("year");
use data.putExtra("keys", data); instead of data.setData(.....)
and get data like;
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
String value = extras.getString("keyName");
}
You should be able be put extras on your intent and get that data through an associated tag. This is how I'm doing it from an Activity's onCreate.
Intent mIntent = new Intent(this, MainActivity.class);
mIntent.putExtra("your_tag", "the string you want to get");
startActivity(mIntent);
finish();
Then in the activity you want that data (the MainActivity or whatever you're calling it)....
Intent intent = getIntent();
String value = intent.getExtras().getString("your_tag");
It may also be worth noting that you can Override the onNewIntent in your MainActivity to handle new Intents coming in, mine currently looks like...
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
Try this
public class AddMovie extends Activity {
String movieTitle, movieYear;
EditText titleField, yearField;
Button save;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_movie);
titleField = (EditText) findViewById(R.id.titleField);
yearField = (EditText) findViewById(R.id.yearField);
save = (Button) findViewById(R.id.saveMovie);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("title_field", titleField.getText().toString);
data.putExtra("year_field", yearField.getText().toString);
setResult(RESULT_OK, data);
finish();
}
});
}
And this
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == request_Code)
{
if(resultCode == RESULT_OK)
{
tempTitle = data.getStringExtra("title_field");
tempYear = data.getStringExtra("year_field");
Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show();
dbAddMovie(tempTitle, tempYear);
}
}
}
You can use a putExtra method on your Intent:
data.putExtra("title", movieTitle);
data.putExtra("year", movieYear);
Then in the Activity that opens, you can get this data as follows:
String title, year;
Intent data = getIntent();
if (data != null) {
title = data.getExtras().getString("title");
year = data.getExtras().getString("year");
}
You can create a new intent and then add your two variables
Intent i = new Intent(getApplicationContext(), asd8.as.hwapp.timemonday.class);
i.putExtra("username",username);
i.putExtra("password", password);
startActivity(i);
And then retrieving the information in your new activity as so;
public void getInformation(){
Bundle extras = getIntent().getExtras();
if (extras != null) {
username = extras.getString("username");
password = extras.getString("password");
}
}

Categories