Calling a function when a Fragment is closed

Asked

Viewed 652 times

0

I have a activity who calls a fragment which is displayed in the form of a DialogAlert. But when that fragment is closed, need a method of Activity be called. What is the best way to do this?

The code of Activity:

package activities;

import classes.Event;
import adapters.EventListAdapter;
import android.app.FragmentManager;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;
import br.bytecode.tarefas.R;
import database.Database;
import fragments.ViewEventFragment;

public class EventsActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_events);
        loadEvents();

        getListView().setOnItemLongClickListener(
                (new OnItemLongClickListener() {

                    @Override
                    public boolean onItemLongClick(AdapterView<?> parent,
                            View view, int position, long id) {

                        Event event = (Event) parent
                                .getItemAtPosition(position);
                        FragmentManager fm = getFragmentManager();
                        ViewEventFragment viewEvent = ViewEventFragment
                                .newInstance(event);
                        viewEvent.show(fm, "frag_view_event");
                        return true;
                    }
                }));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.clear();
        getMenuInflater().inflate(R.menu.menu_events, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id) {
        case R.id.menu_events_new:
            Intent newEvent = new Intent(this, ManagementActivity.class);
            startActivity(newEvent);
            return true;
        case R.id.menu_events_logoff:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    }

    @Override
    public void onResume() {
        super.onResume();
        loadEvents();
    }

    public void loadEvents() {
        Database db = new Database(this);

        EventListAdapter adapter = new EventListAdapter(this, db.readEvents());
        setListAdapter(adapter);
    }
}

The Fragment I want to trigger the function when called is within the method onItemLongClick.

  • Viewnewevent is a DialogFragment? You want to run something when the Dialog is closed by cancellation or once it is closed (no matter how)?

  • Yes, it is a Dialogfragment. I want it to be executed when it is closed, no matter how.

2 answers

2


Do something like this...

public class MeuFragment extends Fragment
{
    private OnFragmentClosedListener onFragmentClosedListener;

    public OnFragmentClosedListener getOnFragmentClosedListener() {
        return onFragmentClosedListener;
    }

    public void setOnFragmentClosedListener(OnFragmentClosedListener onFragmentClosedListener) {
        this.onFragmentClosedListener = onFragmentClosedListener;
    }


    @Override
    public  void onDetach()
    {
        super.onDetach();

        //chama o callback
        if(getOnFragmentClosedListener() != null)
            if(getOnFragmentClosedListener().onFragmentClosed();
    }

    /**
     * Listener para ser chamado quando o fragment for "desamarrado" da activity
     */
    public interface OnFragmentClosedListener{

        public void onFragmentClosed();
    }
}

In your Activity you use this Fragment ok... and implement his callback..

MeuFragment meuFragment = new MeuFragment();
meuFragment.setOnFragmentClosedListener(new MeuFragment.OnFragmentClosedListener() {
    @Override
    public void onFragmentClosed() {

    }
});


    beginTransacttion..... // faz aquele transição para adicionar o fragment a activity

1

For this type of information on the Dialog, the class DialogFragment implements two interfaces: A DialogInterface.OnCancelListener and the DialogInterface.OnDismissListener that provide the information you need. The DialogFragment is already automatically registered in Dialog that is built in-house.

The OnCancelListener is called when canceled by user interaction (backpress or negative button). Already the OnDismissListener is more generic and is called for the same cases of OnCancelListener beyond the call to method dismiss of Dialog.

To use these two Listeners, only by overloading the methods onDismiss(DialogInterface dialog) or onCancel(DialogInterface dialog) that the DialogFragment possesses:

public class ViewEventFragment extends DialogFragment {

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        // Tratar o evento de dismiss do Dialog
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);

        // Tratar evento de cancelamento do Dialog
    }
}

In overloading these methods, I would suggest creating a Listener to be notified at those two points.

  • The ADT is not recognizing the function setOnDismissListener: setOnDismissListener cannot be resolved or is not a field. I need to create this method in ViewEventFragment?

  • Hmm, my mistake, I will change the answer to include the correct code. The problem is that you have to register one of these two Listeners in the Dialog that Fragment. I’ll include that in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.