I am trying to do the following:
Show dialog fragment from an Activity (not Fragment);
Retrieve result in the Activity.
The problem is that onActivityResult in the Activity is not being called.
Here is how I am showing a DialogFragment
DriverRatingDialogFragment dp = new DriverRatingDialogFragment();
// this solution works well in case of showing dialog from a fragment
// but I have to show it from the Activity
// dp.setTargetFragment(getSupportFragmentManager().findFragmentById(R.id.flContent), DriverRatingDialogFragment.REQUEST_CODE);
dp.show(getSupportFragmentManager(), DriverRatingDialogFragment.ARG_RATING);
And here is how I am trying to return result from DialogFragment:
Intent intent = new Intent();
// This solution works well in case of previously setting target Fragment,
// but I have to return result to Activity
// getTargetFragment().onActivityResult(
// getTargetRequestCode(), REQUEST_CODE, intent);
// and with this attempt App crashes
// this.onActivityResult(REQUEST_CODE, Activity.RESULT_OK, intent);
here is where I want to retrieve result in the Activity, but it doesn't get called:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == DriverRatingDialogFragment.REQUEST_CODE) {
// todo
}
super.onActivityResult(requestCode, resultCode, data);
}
What's wrong?
I start the DialogFragment the same way you do.
CreatePostDialog createPostDialog = new CreatePostDialog();
createPostDialog.show(getSupportFragmentManager(), "create_post_dialog");
And I send back data using a public method in the activity rather than with intents.
((MainActivity)mContext).logout(Utils.getLocation(v));
The snippets above are copy pasted examples of my working code. Logout in this case is a public method in MainActivity that takes in a point and does what is needs with it.
you can use callback
1.define a callback in you dialog fragment
public Call mCall;
public interface Call {
void returnData(Data data);
}
2.on dialog fragment life circle
#Override
public void onDetach() {
super.onDetach();
mCall = (Call) getActivity();
mCall.returnData(data);
}
3.activity just implements Call
activity implements DiaFragment.Call
onActivityResult is there to receive informations after startActivityForResult is called, which is not your case.
What kind of datas you need to get from the dialog ?
DialogFragment is usually used in a fragment, not an activity. Look at alert dialog.
Related
I'm using shared element transition when navigate from activity A to activity B.
My issue - calling of SupportFinishAfterTransition in activity B makes OnActivityResult called after OnTransitionEnd with small interval in activity A. So my UI (ImageView) in activity A is "flashing" because of image source updating with interval.
I need OnActivityResult to be called first to update my UI and then play exit transition.
My code:
Activity A:
protected override void OnCreate(Bundle savedInstanceState)
{
// Some code
Window.SharedElementExitTransition.AddListener(this);
}
This code called firstly:
public void OnTransitionEnd(global::Android.Transitions.Transition transition)
{
}
This code called secondly, but I would like to firstly:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
}
Activity B:
SetResult(Result.Ok, intent);
SupportFinishAfterTransition();
use onActivityReenter instead of OnActivityResult. onActivityReenter executes before activity displayed, OnActivityResult executes after activity displayed.
#Override
public void onActivityReenter(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//do your work here
//example of getting data:
int someData = data.getIntExtra("EXTRA_CODE", -1);
}
}
You have to be aware of these things when you want have a transition after returning from startActivityForResult:
i.) you need to specify a windowSharedElementReenterTransition, usually only windowSharedElementEnterTransition and windowSharedElementEnterTransition are specified.
ii.) in the onActivityReenter of the reentered Activity you need to call postponeEnterTransition(), then update your views as desired and call again startPostponedEnterTransition().
onActivityResult is only called when the previous Activity is finished, whereas onActivityReenter is called when the previous Activity is still running (you can get the Intent there to update the UI).
Here is an article which brings light into the dark:
https://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html
I have a main activity that contains 3 fragments. In each fragment I have a listView, on clicking which it is diverted to a new activity.
In the new Activity, I am changing the list view content that is clicked and diverting back to the main activity using intent.
This opens a new main Activity fragment and when I press back button I again get back to the previous Main Activity that was opened earlier.
Save activity's context in static variable like this
public class MainActivity extends AppCompatActivity {
public static MainActivity mainActivityInstance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_staff_login);
//finishing older loaded mainActivity
if(MainActivity.mainActivityInstance!=null){
MainActivity.mainActivityInstance.finish();
}
mainActivityInstance=this;
}
}
From other activities you can finish your older activity like this
if(MainActivity.mainActivityInstance!=null){
MainActivity.mainActivityInstance.finish();
}
Instead of using startActivity use startActivityForResult in your fragment and have onActivityResult to obtain result from new Activity you have started
In your fragment do like this
static final int NEXT_ACTIVITY = 100;
//insert in listview on click
Intent intent = new Intent(context,Activity2.class);
intent.putExtra("key","value");//if needed
startActivityForResult(intent,NEXT_ACTIVITY);
#Override //*Note* you must override this method in you parent Activity in order to reflect the data inside your fragment
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);//while using inside fragment
switch(requestCode) {
case NEXT_ACTIVITY:
if (resultCode == RESULT_OK) {
Bundle res = data.getExtras();
String result = res.getString("param_result");
Log.d("FIRST", "result:"+result);
}
break;
}
}
In your new Activity while finishing/onBackpressed
Bundle data = new Bundle();
data.putString("param_result", "Thanks Thanks");
setResult(RESULT_OK, data);
finish();
For fragments, instead of finish();, you should call
getActivity().finish();
I have one activity A, that has one button and one list view which shows names of books . on click of the button, activity B starts, there user fill the book form and save it . when he press back button , user comes to activity A. Here the book name should be updated in listview. I think I have to write some code in onResume() . Can u please tell me what to write. I am using customised list view.
Start activity B with startActivityForResult() and use method onActivityResult() to restart or process the new data
For example, to start Activity B:
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
Then somewhere in your Activity A class:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
// do processing here
}
}
The other answers should suffice, but onResume() can be called in cases where the activity is resumed by other means.
To simply restart Activity A when user presses back button from Activity B, then put the following inside the onActivityResult:
if(requestCode == 0){
finish();
startActivity(starterintent);
}
And in the onCreate of Activity A, add starterintent = getIntent();
Just remember to initiate the variable with Intent starterintent; somewhere before your onCreate is called.
e.g.
public class ActivityA extends ListActivity {
Intent starterintent;
public void onCreate(Bundle b){
starterintent = getIntent();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 0){
finish();
startActivity(starterintent);
}
}
private void startActivityB(){
String callingActivity = context.getLocalClassName();
Intent newActivity = new Intent(getApplicationContext(),ActivityB.class);
newActivity.setData(Uri.parse(callingActivity));
startActivityForResult(newActivity, 0);
}
}
Then just call startActivityB() from a button click or whatever
YES you are right. Write code in onResume.
When you updated date just call notifyDataSetChanged(); for your ListView adapter
Hope, it help you!
You can either start the activity when user press on Save, and it will fix it for you.
Or if you want to press back:
#Override
public void onResume(){
super.onResume();
list.clear();
list.addAll(getBooks());
adapter.notifyDataSetChanged();
}
How can I detect if an activity came to focus after pressing the back button from a child activity, and how can I execute some code at that time?
One possibility would be to start your child activity with startActivityForResult() and implement onActivityResult() which will be called when you return from the child activity.
js's answer is right, but here is some debugged code.
Declare the request code as a constant at the top of your activity:
public static final int OPEN_NEW_ACTIVITY = 12345;
Put this where you start the new activity:
Intent intent = new Intent(this, NewActivity.class);
startActivityForResult(intent, OPEN_NEW_ACTIVITY);
Do something when the activity is finished. Documentation suggests that you use resultCode, but depending on the situation, your result can either be RESULT_OK or RESULT_CANCELED when the button is pressed. So I would leave it out.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OPEN_NEW_ACTIVITY) {
// Execute your code on back here
// ....
}
}
For some reason, I had trouble when putting this in a Fragment. So you will have to put it in the Activity.
The method you're looking for may be the onResume method you can implements in your mother class ;). You must know that the onResume is also called the first time you launch any activity. Look at the lifecycle of an activity : http://developer.android.com/images/activity_lifecycle.png
Regards,
You can also override both the onBackPressed() method and the onOptionsItemSelected() method and put some logic there. For example I put this into my BaseActivity which all the other Activities extends from:
#Override
public void onBackPressed() {
// your logic
super.onBackPressed();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// your logic
}
return super.onOptionsItemSelected(item);
}
I'm using a TabActivity that calls an ActivityGroup. Tipical approach that brings problems.
Now I'm facing a problem with onActivityResult
in my Activity I have..
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.place_buttontab_community_media);
Button ButtonClick =(Button) findViewById(R.id.addPicture);
ButtonClick.setOnClickListener(new OnClickListener (){
#Override
public void onClick(View view)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// request code
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "hi");
super.onActivityResult(requestCode, resultCode, data);
}
}
unfortunately onActivityResult is never invoked.
You should listen for the result in the ActivityGroup's onActivityResult(). When a result is received, you have to decide which Activity should receive it and call it's onActivityResult() method with the received values.
I recently encountered this, to consolidate Andras' and virtual82's answer and discussion above (Sorry I can't add comment due to my low rep):
If your activity is started inside an ActivityGroup, which is used by TabActivity for example, then only the ActivityGroup activity can receive method calls to onActivityResult when you start an activity with startActivityForResult(Intent intent, int requestCode) from an activity inside the ActivityGroup.
In my situation, I have a TabActivity that contains FragmentActivitys. A Fragment inside a FragmentActivity may start an activity for result.
In this case the Fragment needs to start the activity with TabActivity as the calling activity:
Intent doSomethingIntent = new Intent();
//getActivity() returns the FragmentActivity, so we need to get the TabActivity with getParent()
Activity tabActivity = getActivity().getParent();
doSomethingIntent .setClass(tabActivity , PostCommentActivity.class);
tabActivity.startActivityForResult(doSomethingIntent , DO_SOMETHING_REQ_CODE);
Then in onActivityResult of the TabActivity, pass the result back to the Fragment.
Activity currentActivity = getLocalActivityManager().getCurrentActivity();
//get the FragmentActivity in order to get the Fragment instance
if(currentActivity instanceof FragmentActivity){
FragmentActivity currentFragmentActivity = (FragmentActivity)currentActivity ;
//R.id.l_container is the layout/viewgroup that contains the Fragment
Fragment currentFragment = currentFragmentActivity.getSupportFragmentManager().findFragmentById(R.id.l_container);
if(currentFragment != null){
//pass result to the Fragment!
frag.onActivityResult(requestCode, resultCode, data);
}
}
I realize that ActivityGroup, TabActivity and LocalActivityManager are deprecated in favor of Fragment and FragmentManager, but my project time constraint didn't allow me enough time to fully upgrade.
Hopefully this post will help someone in this situation, instead of the usual "don't do it" kind of response you see else where.