0

I want to display a subfragment when an item in my listfragment gets selected, I have no idea how to do this.

public class MasterFragment extends ListFragment {
    String[] presidents = {
            "Dwight D. Eisenhower",
            "John F. Kennedy",
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(getActivity(), 
                android.R.layout.simple_list_item_1,
                presidents));
    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        Toast.makeText(getActivity(), 
            "You have selected " + presidents[position], 
            Toast.LENGTH_SHORT).show();
        String selectedPresident = presidents[position];

        DetailFragment detailFragment = (DetailFragment) 
            getFragmentManager().findFragmentById(R.id.detailFragment);

        //---if the detail fragment is not in the current activity as myself---
        if (detailFragment != null && detailFragment.isInLayout()) {
            //---the detail fragment is in the same activity as the master---
            detailFragment.setSelectedPresident(selectedPresident);
        } else {
            //---the detail fragment is in its own activity---
            Intent intent = new Intent(getActivity(), DetailActivity.class);
            intent.putExtra("president", selectedPresident);
            startActivity(intent);
        }
    }

Instead of starting a new activity, is there a dialog sub fragment I can use?

Thanks.

user3022474
  • 71
  • 1
  • 1
  • 7

2 Answers2

0

You can display a DialogFragment. Please check the API docs for refrence

http://developer.android.com/reference/android/app/DialogFragment.html

Also, read this thread for information

How to use Dialog Fragment? (showDialog deprecated) Android

Community
  • 1
  • 1
Ajit Pratap Singh
  • 1,299
  • 12
  • 24
0

You can use a DialogFragment:

final String dialogTAG = "Fragment TAG";

final DialogFragment dialogFragment = new DialogFragment();

dialogFragment.show(getFragmentManager(), dialogTAG);
Luis
  • 3,451
  • 1
  • 27
  • 41