Note that there are some explanatory texts on larger screens.

plurals
  1. POCombining Fragments and ActionBar in Android level < 13. Is there a feasible way?
    primarykey
    data
    text
    <p>I would like to write my project in the lowest possible Android version for my reqs, which is 11.</p> <p>But I need Fragment.attach and ActionBars.</p> <p>Since level 11 does not include Fragment.attach I import the support package for v4.</p> <p>But now the problem is that the TabListerner for the ActionTab does not use the v4 Fragment but rather the level 11 Fragment. Casting won't work.</p> <p>Do I really need to switch to level 13 or is there a feasible solution to implement all this in level 11.</p> <p>Here is the code:</p> <pre><code>import android.app.ActionBar; import android.app.Activity; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.support.v4.app.Fragment; import android.app.ActionBar.Tab; import android.app.ActionBar.TabListener; import android.os.Bundle; import android.support.v4.app.Fragment; public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public static class TabListener&lt;T extends android.support.v4.app.Fragment&gt; /* to make sure it take the Fragment from the support package! */ implements ActionBar.TabListener { private Fragment mFragment; private final Activity mActivity; private final String mTag; private final Class&lt;T&gt; mClass; /** * Constructor used each time a new tab is created. * * @param activity * The host Activity, used to instantiate the fragment * @param tag * The identifier tag for the fragment * @param clz * The fragment's Class, used to instantiate the fragment */ public TabListener(Activity activity, String tag, Class&lt;T&gt; clz) { mActivity = activity; mTag = tag; mClass = clz; } /* The following are each of the ActionBar.TabListener callbacks */ public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) { // Check if the fragment is already initialized if (mFragment == null) { // If not, instantiate and add it to the activity mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, mTag); } else { // If it exists, simply attach it in order to show it ft.attach(mFragment); } } /* these are NOT the implementation of the TabListener above, since the use the * * the FragmentTransactionof the support package and not of level 11 * */ public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) { if (mFragment != null) { // Detach the fragment, because another one is being attached ft.detach(mFragment); } } public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) { // User selected the already selected tab. Usually do nothing. } /* these are added since they belong to the above definition of TabListener * * unfortunately the use the FragmentTransaction of level 11, not the one of the support package! * */ @Override public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { } @Override public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { } @Override public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { } } } </code></pre>
    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.
 

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