Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh previous fragment after closing an activity who was started inside an Adapter?

I have a fragment (FragmentA) with a RecyclerView displaying some CardViews. I wanted to make the cardviews clickable , so the solution I found for this was to implement View.OnClickListener in the ViewHolder inside de adapter class. Something like this:

fragmentAAdapter.java

public class fragmentAViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {       

    public fragmentAViewHolder (View itemView) {
        super(itemView);            

        itemView.setOnClickListener(this);
    }     

    @Override
    public void onClick(View v) {
        //do something..
    }
}

The onClick() method gets the data from the clicked cardview and start an activity (InfoActivity) who displays the data.

OnClick() method

@Override
    public void onClick(View v) {            
        Intent intent = new Intent(itemView.getContext(), InfoActivity.class);

        Bundle mBundle = new Bundle();
        mBundle.putParcelable("someData", someData);
        intent.putExtras(mBundle);            
        itemView.getContext().startActivity(intent);
    }

In the InfoActivity I can update and delete the data. After that I want to finish the InfoActivity and refresh the FragmentA to get the new data.

To close the activity and refresh the previous fragment I am trying to use the startActivityForResult(). Like this:

@Override
    public void onClick(View v) {            
        Intent intent = new Intent(itemView.getContext(), InfoActivity.class);       

        ((Activity)  itemView.getContext()).startActivityForResult(intent, 10001);

    }

InfoActivity.java

@Override
    public void onClick(DialogInterface dialog, int which) {        
    setResult(InfoActivity.RESULT_OK);
    finish();
}

FragmentA.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 10001) && (resultCode == InfoActivity.RESULT_OK)) {

        FragmentAfragment  = new FragmentA();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }
}

But when I close the InfoActivity nothing happens.

The problem is that the setResult(InfoActivity.RESULT_OK) returns the result to the adapter class (fragmentAAdapter) not to FragmentA, so the method onActivityResult() in FragmentA is never called.

How do I refresh the FragmentA after close InfoActivity who was started inside the onClick() method in the fragmentAAdapter??


Update - Solution

I overwrote the method onActivityResult() in the activity of the FragmentA. Like this:

MainActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 10001) && (resultCode == 10001)) {

        FragmentA fragment  = new FragmentA();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }
}

and in InfoActivity.java:

@Override
public void onClick(DialogInterface dialog, int which) {        
    setResult(10001);
    finish();
}

Every time it closes an activity and go back to the FragmentA the onActivityResult() in the MainActivity is called, and when the condition if((requestCode == 10001) && (resultCode == 10001)) is true the FragmentA is refreshed.

like image 354
Bruno Santos Avatar asked Aug 18 '16 19:08

Bruno Santos


People also ask

How to refresh a fragment when finish an activity?

Show activity on this post. Now in your fragment you will get event of finishing your activity by overriding onActivityResult() in the fragment. That's it. Hope it will help you.

How do you refresh a fragment?

Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so.

How do I refresh a fragment from another fragment?

You can access another Fragment by its tag: // find your fragment YourFragment f = (YourFragment) getSupportFragmentManager(). findFragmentByTag("yourFragTag"); // update the list view f. updateListView();


1 Answers

In the activity of your fragment you can do something like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    requestCode &= 0xffff;
    for (Fragment fragment : getSupportFragmentManager().getFragments()) {
        if (fragment != null) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

Or you can use a Event bus like: http://square.github.io/otto/

like image 157
AndroidRuntimeException Avatar answered Sep 28 '22 07:09

AndroidRuntimeException