0

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.

David
  • 3,055
  • 4
  • 30
  • 73

2 Answers2

1

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.

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
1

There is no difference between FragmentActivity and Activity except support for lower OS versions for fragment.

Fragment can be used as component in activity. To start a new fragment means you should replace existing fragment with new fragment

Fragment is not like activity So, you can't start new fragment as new activity.

Jaykumar Donga
  • 380
  • 1
  • 3
  • 18