Search code examples
androidandroid-fragmentsandroid-activityexpandablelistviewonbackpressed

Back from Listwiew to MainActivity


I open the ExpandableListView from the Fragment, which is open from MainActivity.

In ListView, a listener is hung at the click of a button in Item.

By clicking this button I want to transfer some value to MainActivity and send it to WebView.

The fragment is opened with beginTransaction () and written to addToBackStack (null)

Everything works fine when I press the back button of the system button, but I just can’t figure out how in my ExpandableAdapter class, with which I open ExpandableListView to call OnBackPress for this Fragment ....

Now I just call MainActivity in onClick and pass the value through putExtra. But this every time reboots my Main Activity, but I do not need it, because it already exists, I just need to return to it.

Call Fragment from MainActivity:

    Fragment fragmentWeb = new MainFragment();
    FragmentManager ft = getSupportFragmentManager();

    ft.beginTransaction().replace(R.id.mainlayout, fragmentWeb, dataFrag).addToBackStack(null).commit();

Part of MainFragment:

public class MainFragment extends Fragment implements OnBackPressed {
        ......

        getActivity().findViewById(R.id.list).setVisibility(ViewGroup.VISIBLE);
        expListView = (ExpandableListView) getActivity().findViewById(R.id.lvExp);
        new DownloadJason().execute();

        private class DownloadJason extends AsyncTask<Void, Void, Void> {
             // Get Json...
        }

    @Override
    protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            listAdapter = new myExpandableListAdapter(getActivity().getApplicationContext(), listHeader, listDataChild, listDataHeader, getActivity());

            expListView.setAdapter(listAdapter);

    }
}

part of myExpandableListAdapter:

public class myExpandableListAdapter extends BaseExpandableListAdapter {
    private Activity _activity;
    private Context _context;
    private List<String> _listDataHeader; // header titles
    private List<Integer> imgHeader;
    private HashMap<String, List<String>> _listDataChild;
    private HashMap<String, List<String>> _listData;
    private Integer imgType1,imgType2,imgType3,imgType4,imgType5;
    private FragmentManager manager;

    public myExpandableListAdapter(Context context, List<String> listDataHeader,
                                   HashMap<String, List<String>> listChildData,
                                   HashMap<String, List<String>> listData,
                                   Activity activity) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        this._listData = listData;
        this._activity = activity;

    }

    ......

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);
        final String markerId = (String) getData(groupPosition, 0);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item2, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);

        ImageButton btnMarker = (ImageButton) convertView.findViewById(R.id.imageButton);
        btnMarker.setImageResource(R.drawable.ic_place_black);


        btnMarker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(_activity, MainActivity.class);
                intent.putExtra("markerId", markerId);
                _activity.startActivity(intent);
            }
        });

        return convertView;
    }

.....

}

Solution

  • Create two interface.

    1.MainFragmentListener for communication with activity/fragment

    2.AdapterClickListener for communication with adapter/fragment.

    You must be initialize to MainFragmentListener like this.

    MainActivity implements MainFragmentListener{
    ..
    }
    

    Create AdapterClickListener at MainFragment. like this.

    MainFragmentListener mainFragmentListener = null;
    

    then give MainFragmentListener to fragment.

    Fragment fragmentWeb = new MainFragment();
    fragmentWeb.adapterClickListener = this;
    

    Create AdapterClickListener at MainFragment and init at onCreatedView() then give this interface to adapter.

    main fragment

     AdapterClickListener AdapterClickListener = null;
    

    adapter constructor

    public myExpandableListAdapter(Context context, List<String> listDataHeader,
                                       HashMap<String, List<String>> listChildData,
                                       HashMap<String, List<String>> listData,
                                       Activity activity,AdapterClickListener adapterClickListener) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
            this._listData = listData;
            this._activity = activity;
            this.adapterClickListener = adapterClickListener;
        }
    

    now you send what you want data to main activity with interface.

    btnMarker.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    adapterClickListener.markerClicked(*****);
                }
            });