Start activity for result not work, between different activities - java

I have the main page where the photo is loaded, depending on what the array on the other page contains, in order to synchronize them I used StartActivityForResult();.
It will works like this: MainActivity has a photo, i press showMore button(open showMoreActivity), at showMoreActivity change text and after i finish showMoreActivity, MainActivity load the new photo depends on new text, but real it doesn`t change photo.
Can you help me? Where the mistake is?
buttonShowMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ShowMore.class);
onStop();
getActivity().startActivityForResult(intent, 1);
}
});
onClick button ShowMore Listener
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
String dataStringExtra =data.getStringExtra("name1");
Picasso.get().load( dataStringExtra + ".jpg").into(imageViewFirstOfCurrentList);
}
onActivityResult method
Intent intent = new Intent();
intent.putExtra("name1", List.get(0).toString());
setResult(RESULT_OK, intent);
finish();
when close showMore Activity

try removing call to onStop() in your onClick() method

Related

Android onActivityResult() not firing

I am aware of the plenty of questions similar to this one, yet none of the solutions mentioned there work for me, and I'm not quite sure why.
I have the following setup:
Launching from MainActivity.java:
Button b = findViewById(R.id.btn_b);
b.setOnClickListener(new View.onClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivityForResult(intent, 0);
}
});
Returning values from Activity2.java:
#Override
public onBackPressed() {
System.out.println("Here"); // this can be seen in the logcat
Intent retIntent = new Intent();
// putExtra some return values
setResult(RESULT_OK, retIntent);
finish();
}
Receiving data back at MainActivity.java:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("Returning"); // this doesn't fire at all
if (data != null) { // I know there will be no other activities, and I want to act on whatever result I get
// extract and do something with data
}
}
I really don't understand what is going on here. Can someone please help explain, and how it can be fixed? I'll be happy to provide more information if needed.
Thank you for your time.
change this line in your MainActivity.java to
startActivityForResult(intent, 1);
and then in the method onBackPressed() call the method setResult like this
setResult(RESULT_OK);
then in the onActivityResult method write this condition.
if (resultCode == Activity.RESULT_OK) {//place your logic here}
Please let me know if this worked for you.

Use onActivityResult to call a function

I have an PopUpActivity which pops up a window and i'm very satisfied with that.
However, I want to be able to return information from the popup window back to the parent activity, but also be able to call a function on the parent activity once the popup window is closed. Could someone help me?
Here is my code in the PopUpActivity:
public void closePopUpAndSendResultBack(){ // and how can i pass data to the previous activity?
//https://developer.android.com/training/basics/intents/result
Intent data = new Intent();
data.putExtra("data", "yo");
//startActivityForResult(data, 1); // gives me the error : android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
setResult(Activity.RESULT_OK, data);
finish();
}
and here is my parent(MainActivity) function that I want to get called once the popUpWindow is closed:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
nameTextField.setText(""); // how can i get Yo inside here?
showTheButton(); // and how can this function get called?
}
}
}}
Also, here is how I make that window Pop up from my Mainactivity:
theOkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pop_up_that_window = new Intent(MainActivity.this,PopActivity.class);
pop_up_that_window.putExtra("first", "Velkommen du der!");
startActivity(pop_up_that_window);
}
});
}
Could someone help me please?
In MainActivity use:
theOkButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent pop_up_that_window = new Intent(MainActivity.this,PopActivity.class);
pop_up_that_window.putExtra("first", "Velkommen du der!");
MainActivity.this.startActivityForResult(pop_up_that_window, 1);
}
});
And still in MainActivity in onActivityResult() use this code:
String yo = data.getStringExtra("data")
nameTextField.setText(yo);

Value of Toast to TextView of second activity

This is my MainActivity.java and I want the results in a text view of another activity? How can I achieve it? Can you Show me with an example please.
public class MainActivity extends AppCompatActivity {
private Button scan_btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scan_btn=(Button)findViewById(R.id.btnQr);
final Activity activity =this;
scan_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
intentIntegrator.setPrompt("Scan");
intentIntegrator.setCameraId(0);
intentIntegrator.setBeepEnabled(false);
intentIntegrator.setBarcodeImageEnabled(false);
intentIntegrator.initiateScan();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null){
if (result.getContents()==null){
Toast.makeText(this,"You cancelled scanning",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this,result.getContents(),Toast.LENGTH_LONG).show();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
This is my Second Activity. Where I want to show the result.
public class DetailActivity extends AppCompatActivity {
private TextView qrResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
qrResult= findViewById(R.id.qrResult);
}
}
If you want I can post my Layout file as well. Thankyou.
You need to create a new Intent object, and add it extra data with intent.putextra(). This method can take a String object as an argument. You need to specify a unique key for that string.Then start the new activity. For example
Intent i = new Intent(context, nextactivity.class)
i.putextra(“stringKey”,yourSstring)
startActivity(i)
Then, in the second activity, you need to get the intent that started that activity (with getIntent), you can use it as early as onCreate.
The getIntent function returns the intent object that started the new activity.
When you have the new intent, you can get the extra string you passed from the old activity, with intent.getStringExtra(“stringKey”)
This allows you to pass simple data between activities. Make sure to use the same key.
You can put data into the intent from your main acivity and the get the intend from the second activity for the data.
For example:
In your MainActivity.class
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
intent.putExtra("result", "Your result text here");
startActivity(intent);
In Your DetailsActivity.class:
Intent intent = getIntent();
String result = intent.getStringExtra("result");
qrResult.setText(result);
You can even send any type of object through intent. Please google it for further information.

Move data by intent without startActivity

I have 2 Activities. On 2nd activity i have data which i want to move by button "Back" to 1st Activity.
Usually i moving by something like this:
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pass=editText2.getText().toString();
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivity(intent);
}
});
But now i dont want to start Activity, instead of this i want close Activity, so i need to use finish()
Currently i created this, but no working:
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, FirstActivity.class);
intent.putExtra("number", numberOfTrue);
finish();
}
});
I need something more in this code, but i dont know what.
Start a new activity with
Intent intent = new Intent(FirstActivity.this, MapsActivity.class);
intent.putExtra("pass_value", pass);
startActivityForResult(intent,1)
You can use setResult method to acheive your desired result
Intent output = new Intent();
output.putExtra("number", numberOfTrue);
setResult(Activity.RESULT_OK, output);
finish();
get Your result in FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
int num = data.getIntExtra("pass_value");
}
}
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
Intent intent = new Intent(this, FirstActivity.class);
intent.putExtra("number", numberOfTrue); }});
You need to open by function startActivityForResult
startActivityForResult(Intent intent, int requestCode)
And get the data returned in the function
onActivityResult
As an example you can see a face to the camera for a picture

onActivityResult shows null for intent-data [duplicate]

I'm new to android . This might be the simplest question of all !! but I couldn't figure out whats gone wrong here,
I was trying to create a basic example for passing values through intent.So I need to pass data to Main Activity when I close my Second Activity here is the code
IntentTest1(MainActivity)
public void onClick(View arg0) {
// TODO Auto-generated method stub
MyClass.myToast("Clicked",getApplicationContext());
Intent myIntent = newIntent(getApplicationContext(),SecondPage.class);
startActivityForResult(myIntent,0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(requestCode == 0 && resultCode == RESULT_OK)
if(data.hasExtra("title"))
{
MyClass.myToast(""+resultCode+""+requestCode, getApplicationContext());
String str = data.getExtras().getString("title").toString();
titleText.setText(str);
}
super.onActivityResult(requestCode, resultCode, data);
}
SeconPage
public void finish()
{
Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
returnIntent.putExtra("Welcome Back!!","title");
setResult(RESULT_OK, returnIntent);
// below was for tosting and it works!!
MyClass.myToast("finally",getApplicationContext());
super.finish();
}
I think there is some mistake in receiving the intent ,I couldn't figure out.
Answers and Advises are needed
thanks in advance
The first problem is when you create your Intent to send back to the first Activity. Since you are using startActivityForResult() you want to use an empty constructor. So change
Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
to
Intent returnIntent = new Intent();
The second problem is that you have your key/value pair backwards in your Extras. The key, which is what you look for with getStringExtra() etc... should be the first in the pair. So this
returnIntent.putExtra("Welcome Back!!","title");
should be
returnIntent.putExtra("title", "Welcome Back!!");
Off-topic
I would consider using relevant names as your params. For example, I would change your onClick() from
public void onClick(View arg0)
to
public void onClick(View view)
view, v, or something similar makes more sense since the argument actually is a view and it will be more readable
I would also recommend using the Activity Context for your Intent which you can get from the argument (the View) passed into onClick(). So change it to
public void onClick(View v)
{
MyClass.myToast("Clicked",getApplicationContext());
Intent myIntent = newIntent(v.getContext(),SecondPage.class);
startActivityForResult(myIntent,0);
You have to use
if(data.hasExtra("Welcome Back!!"))
instead of
if(data.hasExtra("title"))
in onActivityResult. Welcome Back!! is the key and title is the value for that key in your extras.
try this code:
Intent returnIntent = new Intent(getApplicationContext(),Intenttest1.class);
returnIntent.putExtra("Key name here in ur case title","Value name");
setResult(RESULT_OK, returnIntent);
// below was for tosting and it works!!
MyClass.myToast("finally",getApplicationContext());
super.finish();
}

Categories

Resources