Android startActivityForResult from AsyncTask with context - android

I have an activity, it's the sender. So when I push a button in this activity, it creates an AsyncTask, and when I get an answer from the server I have only Context from the sender activity.
Sender:
It starts AsynTask.
AsyncTask (mContext is context from Sender class):
Intent i = new Intent();
i.setClass(mContext, SecondActivity.class);
Sender act = (Sender)mContext;
act.startActivityForResult(i,1);
And in SecondActivity:
#Override
public void onPause()
{
super.onPause();
Intent returnIntent = new Intent();
returnIntent.putParcelableArrayListExtra("result", data);
setResult(RESULT_OK, returnIntent);
finish();
}
In the Sender, I try to get result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
ArrayList<MyClass> result = data.getParcelableArrayListExtra("result");
addFriends(result);
}
}
}
But the requestCode is equal to 0 (when I am started it with 1) and resultCode = 0. I don't know why.
I hope someone can help me!
Thanks!

onPause method it`s not the good place to return the result to the activity. Try to return it in the another place - on some button action or in onBackPressed method.

In the 2nd activity use onBackPressed, and its works!
I dont know why dont work in onPause
#Override
public void onBackPressed()
{
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
super.onBackPressed();
}

Related

How can I pass parameters between two Activity but one must only support the first one

I have an activity that very quickly has to process data and then return to the previous activity, I give an example: I have a MainActivity class that passes information through Intent to another Loader class, this processes the data and sends it back to the MainActivity. I don't know how to put this procedure into practice...
From your MainActivity call the TargetActivity using startActivityForResult()-
For example:
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra(); // sent your putExtra data here to pass through intent
startActivityForResult(intent, 1000);
In your intent set the data which you want to return back to MainActivity. If you don't want to return back any data then you don't need to set any data.
For example:
In TargetActivity if you want to send back data:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", result);
setResult(Activity.RESULT_OK, returnIntent);
finish();
If you don't want to return data:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
Now in your MainActivity class write following code for the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
// Do your task here.
}
}
}
//do Some work
Intent i = new Intent(this,MainActivity2..class);
startActivityForResult(i,12);
}
In MainActivity2.class
// after your work complete
Intent i =new Intent();
i.putExtra("result",true);// any data you want to pass
setResult(RESULT_OK,i);
After this we handle result
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
switch(requestCode){
case 12:
if(resultCode == Activity.RESULT_OK){// onsuccess do something
boolean isSucces = data.getBooleanExtra("result",false);
if(isSuccess)// perform action
{// show toast}
}
}
}
I find the best to use callbacks.
in Loader:
Create inner class
MyCallback callback;
viod setCallback(MyCallback callback){
this.callback = callback;
}
viod onStop(){
callback = null;
}
interface MyCallback{
void doSomething(Params params);
}
in MainActivity:
implement MyCallback
set reference in onCreate
Loader loader = new Loader();
loader.setCallback(this);
override method doSomething()
#override
void doSomething(Params params){
//do your thing with the params…
}
when the job is done inside Loader call MainActivity:
callback.doSomething(params);
destroy reference inside MainActivity in onStop()
loader.onStop();
By the help of android startActivityForResult() method, you can get result from another activity.
By the help of android startActivityForResult() method, you can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).
In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
MainActivity.java
public class MainActivity extends Activity {
TextView textView1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with
requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText editText1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(Activity.RESULT_OK,intent);
finish();//finishing activity
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}

startActivityForResult and setResult with RESULT_OK does not work

I'm not sure if this is expected behavior, but if I do the following in OneActivity to launch TwoActivity:
Intent intent = new Intent(this, TwoActivity.class);
startActivityForResult(intent, RESULT_OK);
And in TwoActivity when I pass back to OneActivity:
Intent resultIntent = new Intent();
resultIntent.putExtra(SOURCE, TAG);
setResult(RESULT_OK, resultIntent);
finish();
With the above code and after overriding onActivityResult in OneActivity nothing happens. onActivityResult doesn't even seem to be called. If, however, I change RESULT_OK to 0, it works.
Is this expected? Has anyone else experienced that?
Check out the docs definition of the startActivityForResult method. It says:
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
So your request code should be >= 0. If you check the value of the RESULT_OK response code, it's -1. It's important to note that the request code isn't the same as the result code. The request code serves you to identify the request the result is for, and the result code tells you if the request was successful or not.
You are confusing two different concepts:
The requestCode (the second parameter to startActivityForResult) is a unique ID you assign that can be any positive integer.
The resultCode (the first parameter to setResult) must be one of the constants in the Activity class as seen in the setResult documentation
You'll note that your onActivityResult receives both the requestCode you pass into startActivityForResult and the resultCode that you've set in setResult - make sure you are comparing the right numbers in your onActivityResult
please check your code with this sample:
MainActivity:
public class MainActivity extends Activity {
TextView textView1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1=(TextView)findViewById(R.id.textView1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
});
}
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
}
and then SecondActivity:
public class SecondActivity extends Activity {
EditText editText1;
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
editText1=(EditText)findViewById(R.id.editText1);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String message=editText1.getText().toString();
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();//finishing activity
}
});
}

Make an activity A wait an activity B finish to continue

I have an activity A with this fuction:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
checkSettings();
}
Is there any way to make the activity A to wait for the activity B ("Settings.class") finish to execute the fuction "checkSettings();" ?
Thanks
Edit: I may have misunderstood your question. If you want to run checkSettings() function in B then you need to define and call that function in your B activity.
Otherwise, if you want to wait for activity B to end before running checkSettings() then copy the following code.
In A:
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 1);
}
then also in A Override onActivityResult method.. this gets called when B ends:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
checkSettings();
}
In your Activity A write
public void onSettingsClick(View view){
Intent intent = new Intent(this, Settings.class);
startActivityForResult(intent, 100);
}
and also in you Activity A override onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
if(requestCode==100){
checkSettings();
}
}
}
and in your Activity B
when you want to finish that activity write that piece of code there
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
Another approach to doing this since you are launching a separate activity is call check settings in onResume. This way since you're not having to pass any data and if the are other reasons you may want to refresh or recheck your settings, it will do so any time the activity is brought back to the top.
override fun onResume() {
super.onResume()
checkSettings()
}

onActivityResult not being called inside FragmentActivity that contains Android Map view fragment

I have a fragment activity that displays an Android V2 Map. Inside I also have a onActivityResult used to handle the intent Extras that needs to be passed from the calling activity
public class DisplayMap extends FragmentActivity implements LocationListener {
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.map);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
Log.v("TEST", "********************************************");
}
}
Here is the code form the activity that calls it.
Intent i = new Intent("com.example.DisplayMap");
setResult(RESULT_OK, i);
startActivityForResult(i, 2014);
But somehow the onActivityResult is not called inside.
Thanks in advance.
Dennis
onActivityResult() needs to be in the calling activity, it retrieves the result, as the name suggests.
To return a result from the called activity you'll need to use setResult() and finish that activity:
called activity:
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
with RESULT_OK being passed as the resultCode parameter to onActivityResult() and i as intent
calling activity:
Intent i = new Intent("com.example.DisplayMap");
startActivityForResult(i, REQUEST_CODE);
and to receive the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == REQUEST_CODE){
if (resultCode == RESULT_OK){
// RESULT OK, take the returned extras from intent and use them
}
}
}

Show a button on a previous activity when going back from current activity

I want to visible a edit button on my previous activity by going back using back button. But when I am using
#Override
public void onClick(View arg0) {
// finish the current activity
finish();
}
on my current activity, it is going back to previous activity, but edit button doesn't become visible.
If I am using onResume on previous activity
#Override
protected void onResume() {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
then it's always visible, no matter if the activity is resuming or created for the first time.
I am new in Android development, please help me to solve this problem.
Ok,
When you start the Activity, instead of startActivity(intent) use startActivityForResult(intent, 1989) (The 1989 can be whatever int you want).
When you return from the new Activity, before calling finish() do the following:
Intent returnIntent = new Intent();
returnIntent.putExtra("resultBool", true);
//Null checks, not strictly neccescary
if (getParent() == null)
{
setResult(Activity.RESULT_OK, returnIntent);
}
else
{
getParent().setResult(Activity.RESULT_OK, returnIntent);
}
finish();
Then in your first Activity override onActivityResult() like so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// The int you initially used
if (requestCode == 1989)
{
if (resultCode == RESULT_OK)
{
boolean result = data.getBooleanExtra("resultBool", false)
if (result)
//Show the button now
}
}
}
You can make a boolean variable which can be set to true when you finish and make a check with this boolean in your onResume method
Override onRestart() method
#Override
protected void onRestart () {
super.onResume();
btnEdit.setVisibility(View.VISIBLE);
}
Or start Activity with
startActivityForResult (Intent intent, int requestCode)
method and override
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult (requestCode,resultCode,data);
btnEdit.setVisibility(View.VISIBLE);
}

Categories

Resources