Call a function when a fragment is closed

0

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

The Activity code:

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 to be called is within the onItemLongClick method.

    
asked by anonymous 24.06.2014 / 02:10

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 its 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
    
26.06.2014 / 05:09
1

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

OnCancelListener is called when canceled by user interaction ( backpress or negative button). OnDismissListener is more generic and is called for the same cases of OnCancelListener besides calling method dismiss of Dialog .

To use these two Listeners , only through overloading methods onDismiss(DialogInterface dialog) or onCancel(DialogInterface dialog) that DialogFragment has:

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

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

    
24.06.2014 / 03:36