Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Android provides a simple way to switch between activities using a TabHost. You can also use it to switch between Fragments as explained under <a href="http://developer.android.com/reference/android/app/TabActivity.html" rel="nofollow">TabActivity</a>. Alternatively, you can add a FrameLayout to your activity, programmatically instantiate the fragments and attach/show/hide them when needed.</p> <p>Your res/layout/main.xml would look like this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="match_parent" android:id="@+id/mainframe"&gt; &lt;/FrameLayout&gt; </code></pre> <p>And assuming the v4 support library is used, your activity would look like this:</p> <pre><code>public MyActivity extends FragmentActivity { private Fragment mListFrag; private Fragment mGameFrag; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mListFrag = new MyListFragment(); mGameFrag = new MyGameFragment(); FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction() .add( R.id.mainframe, mListFrag, MyListFragment.class.getName()) .add( R.id.mainframe, mGameFrag, MyGameFragment.class.getName()) .detach(mGameFrag) .commit(); fm.executePendingTransactions(); } public void showList() { getSupportFragmentManager().beginTransaction() .hide(mGameFrag) .show(mListFrag) .commit(); } public void showGame() { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); if (mGameFrag.isDetached()) { ft.attach(mGameFrag); } ft.hide(mListFrag).show(mGameFrag).commit(); } } </code></pre> <p>Observe that MyGameFragment.onCreateView isn't called until it is first attached. After that, hiding and showing the fragments allows the user to switch without delay.</p> <p>Edit: I realise now you wanted 2 activities with their own button. I've updated the code to reflect this. From the OnClickListeners simply call the relevant activity functions like so:</p> <p><code>((MyActivity) MyListFragment.this.getActivity()).showGame();</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload