Make an activity A wait an activity B finish to continue - android

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()
}

Related

How to pass same class method inside onActivityResult() in android?

I have one method - addEntryData. I want to pass the addEntryData method inside onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent );
// check if the request code is same as what is passed here it is 2
if(requestCode==2) {I want ot put my addEntryData method here.}
}
public void addEntryData(SMSForwardEntry smsForwardEntry)
{
smsForwardEntries.add(smsForwardEntry);
smsForwardAdapter.notifyDataSetChanged();
saveData();
Analytics.track(AnalyticsEvents.SMS_FORWARD_ADDED);
}
you need to use startActivityForResult()
Example:
Intent i = new Intent(this, SecondActivity.class);
// add data that you want to pass to other activity in to Intent
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);
and in SecondActivity you can get same Intent using getIntent().
And from SecondActivity return result using:
setResult(RESULT_OK); .....
and handle it in onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent );
// check if the request code is same as what is passed here it is 2
if(requestCode==2) {
addEntryData(smsForwardEntry);//Add like this
}
}
public void addEntryData(SMSForwardEntry smsForwardEntry) {
smsForwardEntries.add(smsForwardEntry);
smsForwardAdapter.notifyDataSetChanged();
saveData();
Analytics.track(AnalyticsEvents.SMS_FORWARD_ADDED);
}

Bundles or PutExtra when executing finish()

I would like to ask if there is a way to put Extras or Bundle when you call finish() method.
For example just like calling an Activity you can do this:
Intent intent = new Intent(view.getContext(), AddListing.class);
intent.putExtra("user_id", userID);
startActivity(intent);
is it possible to use it in finish()?
Thanks.
If you are looking to pass back results when an Activity is destroyed, what you should be looking for is : startActivityForResult.
There are two variants of startActivityForResult() , which allows to pass bundle as well in case of a necessity:
public void startActivityForResult (Intent intent, int requestCode)
public void startActivityForResult (Intent intent, int requestCode, Bundle options)
See this example to understand the concept better:
MainActivity.java:
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 from 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:
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
}
});
}
No finish() method is available with intent as it's parameter.If you want to pass some data back to calling Activity you can use setResult(int resultCode, Intent intent) before calling finish(). In this method you will need to pass bundle or extras in intent. And accept it in onActivityResult method in calling activity. This is the only way to pass data back to calling Activity. You can refer this link https://developer.android.com/training/basics/intents/result.html
Define the userId-->after the intent initiation ,
Example,
Intent i = new Intent(1st.this, 2nd.class);
String sDate = Dpcheckin.getText().toString();
String eDate = Dpcheckout.getText().toString();
String roomtext=RoomText.getText().toString();
i.putExtra("date1", sDate);
i.putExtra("date2", eDate);
i.putExtra("roomtext",roomtext);
startActivity(i);
where,
Mainly you need to define 2 classes inside the Intent,
1st one is the class from which you are sending the data, with(.this)extension.
2nd one is the class you are receiving the data , with (.class)extension.

Android startActivityForResult from AsyncTask with context

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();
}

Calling startActivityForResult never pass to onActivityResult

I'm calling startActivityForResult from a child activity to MainActivity but never pass for onActivityResult
secondActivty this is the call
private void bringMainActivityToTop() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bun= new Bundle();
bun.putParcelable("value",new ListData("HelloDummy",longi,lati,"data"));
intent.putExtra("bundle",bun);
setResult(RESULT_OK, intent);
startActivityForResult(intent, 111);
}
And I want see the call in here on the MainActivity I don't care the code I just want to see the call.
#Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
System.out.println("HelloWorld");
Log.i(TAG,"HelloWorld");
}
and the manifest
<activity
android:name="com.test.mppqvat.activity.MainActivity"
android:label="#string/main_act_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
onActivityResult() belongs to your second activity (if its not then it must to), not the MainActivity. onActivityResult() method wont trigger in MainActivity just cuz MainActivity is started for result. Do you realize that?
Also your MainActivity if being started for result must SET the result using setResult() method and than it has to be finished using finish()method, that way the result will be passed from your MainActivity to your caller SeconActivity activity and its onActivityResult() will be fired.
MainActivity.java(in onCreate)
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
}
});
(outside onCreate)
#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");
//use resulted message
}
}
SecondActivity.java(in onCreate)
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
}
});
Your called Activity must call setResult(Activity.RESULT_OK) or setResult(Activity.RESULT_OK, data)

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
}
}
}

Categories

Resources