I need to start Activity in onCreate of another Activity and wait until activity2 finish. How to do that?
public class Activity1 extends Activity{
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
//do some code
startActivity(Activity2)
//wait until activity2 finish
//another code which can be done after activity2 finish
}
...
}
Or I need to do another code in OnActivityResult async way??
You should try this way...
public class FirstActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(SecondActivity.this, getApplicationContext());
int requestCode = 0;
startActivityForResult(i,requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// do your stuff here after SecondActivity finished.
}
}
Hope this helps you.
You can use startActivtyForResult method to start the activty2 then when onActivityResult of activty1 called call setContentView for the first one
You can start activity2 like startActivityForResult(intent, requestCode).
And do what ever you want to do in onActivityResult(int arg0, int arg1, Intent arg2) on finsh activity2.
You can use startActivityForResult and then catch the result in OnActivityResult
Doc
You should use startActivityForResult(intent, requestCode);
Related
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.
I am trying to call another app through intent, the app is called but the onActivityResult is not being called. Could someone please help me on this?
Below is my code:
public class EncryptCommandActivity extends Activity{
EncryptionFactory encryptionFactory = new EncryptionFactory();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.encrpyt_command_activity);
ActivityContexts.setEncryptCommandActivityContext(this);
Intent intent = new Intent("asd.com.qweapi.MAIN_ACTIVITY");
Bundle bundle = new Bundle();
bundle.putInt("Function", 1006);
bundle.putString("MSG", MQTTFactory.getById());
intent.putExtras(bundle);
startActivityForResult(intent, 0);
finish();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent pData)
{
super.onActivityResult(requestCode,resultCode,pData);
Log.d("Encrypt","Inside"); //not called
Toast.makeText(ActivityContexts.getMainActivityContext(),"encrypt", Toast.LENGTH_LONG).show(); //not called
}
}
You should remove finish() in onCreate() because it will finish activity and then it's no longer to exist, since can not fire onActivityResult()
move finish() to onActivityResult from onCreate
I have an activity A that calls an activity B (FragmentActivity) using startActivityForResult(). Before activity B finishes by calling finish(), I use setResult(). In spite of all of this, onActivityResult() is not called. I'm not sure what I'm doing wrong as I'm not doing anything unconventional, or if this is an Android bug.
Seeing some of the suggestions on here, I've checked to see that I don't have android:launchMode="singleInstance" nor android:noHistory="true" set anywhere in my manifest file.
Calling activity B from A:
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivityForResult(intent, MY_RESULT);
Finishing activity B:
Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();
Overriding onActivityResult() in activity A:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Toast.makeText(getApplication(), "toast", Toast.LENGTH_LONG).show();
}
Edit 1
I commented out nearly all of activity B's code (see below), leaving A's intact and the problem remained. So, A is definitely doing something strange that causes the problem. Worse, I have done this with other activities (using the same code) without any problems! A extends ExpandableListActivity, although I don't understand how any of that code would cause issues in onActivityResult(). Logcat isn't giving me anything useful.
This is the code tested for activity B, for posterity's sake (though it seems to be that the problem is in A):
public class ActivityB extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activityb);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
try this and verify with your code:
ActivityA.java
public class ActivityA extends Activity{
private static final int MY_RESULT = 0;
private Button btnStart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
btnStart=(Button)findViewById(R.id.btnstart);
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, MY_RESULT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(getApplication(), data.getStringExtra("data"), Toast.LENGTH_LONG).show();
}
}
ActivityB.java
public class ActivityB extends FragmentActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Intent intent = new Intent();
intent.putExtra("data", "Hello from ActivityB");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
Note : make sure you have added both the activity in your manifest file.
OK, thanks to everyone that pointed me in the right direction. I found the solution. The issue was that I was doing startActivityForResult() in a DialogFragment so I should have done
getActivity().startActivityForResult()
instead of just
startActivityForResult()
then it works as expected.
As mentioned in several answers, calling requestWindowFeature(Window.FEATURE_NO_TITLE) should be before super.onCreate(...) and setContentView(...).
However, I want the screen's title to appear when the activity is created, and to disappear only after returning from another activity.
I tried this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
And I'm getting the android.util.AndroidRuntimeException: requestFeature() must be called before adding content exception.
// try this way,hope this will help you....
Note : i think what you trying do is not possible so try this alternative.
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
if(!getIntent().getBooleanExtra("isTitleShow",true)){
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this condition for stop to call SecondActivity after one time call
if(getIntent().getBooleanExtra("isTitleShow",true)){
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
finish();
}
}
}
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("isTitleShow",false);
startActivity(intent);
}
}
I have the following workflow:
startActivityForResult(Activity1)
finish() called on Activity1 (when pushing a button)
onActivityResult() ==> startActivityForResult(Activity2)
===> Activity2.onCreate() is called before Activity1.onStop()
Why I have that?
Edited:
Here is the code:
1- MainActivity.java
// On click on a button
public void start(View view) {
Intent activityIntent = new Intent(this, Activity2.class);
startActivityForResult(activityIntent, 0);
}
protected void onActivityResult(int requestCode,
int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Intent activityIntent = new Intent(this, Activity3.class);
startActivityForResult(activityIntent, 0);
}
2- Activity2.java
// A button to finish the activity
public void stop(View view) {
finish();
}
#Override
protected void onStop() {
super.onStop();
}
3- Activity3.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
}
Because of the lifecycle. onStop isn't called until after an Activity is removed from view. So onStop won't be called until something else is blocking it from the user- activity2 in this case. That means Activity2 will already have to have been created, because you can't block another activity if you don't exist.