Open new Fragment from FragmentActivity

David

I have MainActivity that extends FragmentActivity and I also have ConnectionListFragment that contains List of connection ID's. And when user tap on connection ID I want to open new LogFragment (that extends Fragment) and give more details about that connection. But the problem is that when user select connection ID fragment never show's. Here's my code My MainActiviy

public class MainActivity extends FragmentActivity implements LogTextUpdater, LogDetail{

    private PagerAdapter mPagerAdapter;
    private static int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UiFragment.setLog(this);
        ConnectionListFragment.setLogDetail(this);

        initialisePaging();
    }

    private void initialisePaging() {
        List<Fragment> fragments = new Vector<>();
        fragments.add(Fragment.instantiate(this, UiFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, ConnectionListFragment.class.getName()));
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(mPagerAdapter);
    }

    @Override
    public void updateText(String sender) {
        //LogFragment.readFromDatabase(sender);
        ConnectionListFragment.updateList();

        Toast.makeText(this, sender, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startFragment(String connectionID) {
        Toast.makeText(this, "Start " + connectionID, Toast.LENGTH_SHORT).show();

        LogFragment logFrag = new LogFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        logFrag.setConnectionID(connectionID);
        ft.replace(R.id.sample_content_fragment, logFrag);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }
}

my main_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    <FrameLayout
        android:id="@+id/sample_content_fragment"
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0px" />
</LinearLayout>

and here's my ConnectionListFragment

public class ConnectionListFragment extends Fragment{

    private static SQLiteDatabase db;
    private static SQLiteOpenHelper helper;
    private static Context context;
    private static ListView listView;
    private static SimpleCursorAdapter adapter;
    private static LogDetail logDetail;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();

        adapter = new SimpleCursorAdapter(context, R.layout.connection_list, null,
                new String[]{Constants.SESSION_ID}, new int[]{R.id.connectID}, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_connection_list, container, false);
    }

    public static void setLogDetail(LogDetail logTextUpdater){
        logDetail = logTextUpdater;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        listView = (ListView) view.findViewById(R.id.listView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedID = ((TextView) view.findViewById(R.id.connectID)).getText().toString();
                //Toast.makeText(getActivity(), selectedID, Toast.LENGTH_SHORT).show();
                logDetail.startFragment(selectedID);
            }
        });
    }

    public static void updateList(){
        new ConnectionListID().execute();
    }

    public static class ConnectionListID extends AsyncTask<Void, Void, Cursor> {

        @Override
        protected Cursor doInBackground(Void... params) {
            helper = new Database(context);
            db = helper.getReadableDatabase();
            return db.query(true, Constants.TABLE_NAME, new String[]{"_id", Constants.SESSION_ID},
                    null, null, Constants.SESSION_ID, null, null, null);
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);

            adapter.changeCursor(cursor);
            listView.setAdapter(adapter);
        }
    }
}

So what can be wrong with my code. I guarantee that startFragment method is called when user tap on one of the list item because I see Toast on my device.

Derek Fung

According to your code, the container is of height 0px, so it is not visible.

<FrameLayout
    android:id="@+id/sample_content_fragment"
    android:layout_weight="2"
    android:layout_width="match_parent"
    android:layout_height="0px" />

From your usage, I would suggest you to create a new Activity with only this FrameLayout, with height match_parent to contain your LogFragment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call FragmentActivity from Fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Import text from fragmentactivity to fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

Open a new activity with a button from a static fragment?

From Dev

How to open a new fragment from the navigation drawer?

From Java

How do I open a new fragment from another fragment?

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

Why doesn't my application open new Activity from fragment?

From Dev

Is it possible to open new activity from a fragment without trigger button?

From Dev

Open Fragment from fragment error

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

From Dev

How to open new Activity from a fragment java file by clicking the button on the fragment xml file?

From Dev

Open Fragment From Activity

From Dev

Open fragment from activity?

From Dev

Open ActionBarActivity from fragment

From Dev

fragment run before fragmentActivity

From Dev

Android problems with fragment and fragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

From Dev

Open fragment from another fragment- Error

Related Related

  1. 1

    Call FragmentActivity from Fragment

  2. 2

    refresh fragment UI from fragmentActivity

  3. 3

    Import text from fragmentactivity to fragment

  4. 4

    refresh fragment UI from fragmentActivity

  5. 5

    Open a new activity with a button from a static fragment?

  6. 6

    How to open a new fragment from the navigation drawer?

  7. 7

    How do I open a new fragment from another fragment?

  8. 8

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  9. 9

    Android - How to access a Fragment from a FragmentActivity

  10. 10

    (Android) how to call method in fragment from fragmentActivity

  11. 11

    Android - Back button from FragmentActivity to another Fragment

  12. 12

    Fragment being launched from FragmentActivity not showing up

  13. 13

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  14. 14

    How to pass parameter to Fragment from FragmentActivity

  15. 15

    Why doesn't my application open new Activity from fragment?

  16. 16

    Is it possible to open new activity from a fragment without trigger button?

  17. 17

    Open Fragment from fragment error

  18. 18

    How to call fragment from activity without using fragmentactivity?

  19. 19

    How to call a fragment layout from the FragmentActivity to avoid NullPointerException

  20. 20

    How to open new Activity from a fragment java file by clicking the button on the fragment xml file?

  21. 21

    Open Fragment From Activity

  22. 22

    Open fragment from activity?

  23. 23

    Open ActionBarActivity from fragment

  24. 24

    fragment run before fragmentActivity

  25. 25

    Android problems with fragment and fragmentActivity

  26. 26

    Refresh Fragment in FragmentActivity

  27. 27

    Fragment transaction with a FragmentActivity instead of a Fragment

  28. 28

    How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

  29. 29

    Open fragment from another fragment- Error

HotTag

Archive