0

I have an activity:

public class MainActivity_with_Fragment extends RoboFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new SettingsFragment())
                    .add(R.id.container, new AddReminderFragment())
                    //.add(R.id.container, new ReadRemindersFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

with this layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.example.stopcall.app.activities.dele_MainActivity2_with_Fragment"
             tools:ignore="MergeRootFrame"/>

I want it to host 3 fragments separately (showing only one every time):

Add Item to DB fragment

Read Items from the DB

Setting fragment

How would you suggest to design and implement this? what is the proper way?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • You are adding both the fragments in same container. If you need to show only one at a time then user `replace` method not `add`. But if you need to show both at same time then add them in different containers. Please see my answer – Rohit5k2 Jan 31 '15 at 21:42

1 Answers1

0

You are adding both the fragments in same container. If you need to show only one at a time do this.

getSupportFragmentManager().beginTransaction()
   .add(R.id.container, new SettingsFragment()).commit();

getSupportFragmentManager().beginTransaction(
   .replace(R.id.container, new AddReminderFragment()).commit();

But if you need to show both together then create a new container (say R.id.container_two) and do this

getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new SettingsFragment()).commit();
getSupportFragmentManager().beginTransaction()
    .add(R.id.container_two, new AddReminderFragment()).commit();
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57