deeplight
deeplight

Reputation: 21

Android navigation using fragments and java.reflect

I was thinking of a way to navigate through different screens in an android application, using fragments generated by the Java reflection API. But i wonder if this method is the right thing to do.

The problem : i have many screens in my application, all of them reachable from a NavigationDrawer or the main menu in my first activity (HomeActivity). Many of theses screens use similar UI behavior in the header or footer part of the screen (for example, the header part of the screen is either an authentication activity access or a search activity access). So I want to use fragments to avoid code duplication. The idea : having only one FragmentActivity to display all theses screens, with fragments changed by the selection of a screen.

I have made a diagram to represents that solution : http://hpics.li/8689dab

Once a screen has been selected (using a button in the main menu or in the NavigationDrawer), a set of Class is send as Intent to the FragmentActivity. We generates fragments using theses Class objects and display them in the FragmentActivity. We repeat the process each time another screen is selected, without keeping the fragments to avoid overloading the memory.

My question is : will the java reflection impact greatly the navigation using this technique ?

Thank you in advance.

Upvotes: 2

Views: 735

Answers (1)

HenryChuang
HenryChuang

Reputation: 1459

it is feasible, just pass Fragment's class name to FragmentActivity.

FragmentActivity :

    public class MyFragmentActivity extends FragmentActivity {
        public static Fragment fragment;
        public static final String FRAGCLASS = "fragmentclass";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(R.layout.fragment_layout);
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            //get Fragment's class name
            String fragment_className = this.getIntent().getStringExtra(FRAGCLASS);

            //Initialize fragment
            Class<?> c = null;
            try {
                c = Class.forName(fragment_className);
                this.fragment = (Fragment) c.newInstance();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            this.fragment.setArguments(this.getIntent().getExtras());

            //R.id.view from R.layout.fragment_layout
            fragmentTransaction.add(R.id.view, this.fragment,"tag" + fragment_className);
            fragmentTransaction.commit();
        }
}

fragment_layout.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout 
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true" />
    </RelativeLayout>

Upvotes: 1

Related Questions