Juan Carlos Beltran
Juan Carlos Beltran

Reputation: 3

Orientation restarts my app

Im new to programing, I have a naviagation drawer with fragments. When i rotate my device it "restarts the app" or calls the Home fragment. Im not sure how to fix this im gessing it is related to the super.onCreate (savedInstance) but havent found how to implement it. I also tryed with the onConfigurationChanged method on my manifest with no luck. I ve been copying code from different tutorials to make my app work so im not sure if the code im posting should be the one I should post. Thanks.

public class MainActivity extends FragmentActivity {


    private DrawerLayout mDrawerLayout;
    ImageView home;
    Fragment fragment = null;
    TextView appname;
    ExpandableListView expListView;
    HashMap<String, List<String>> listDataChild;
    ExpandableListAdapter listAdapter;
    List<String> listDataHeader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fontPath = "neue.ttf";
        setContentView(R.layout.activity_main);
        home = (ImageView)findViewById(R.id.home);
        home.setOnClickListener(homeOnclickListener);
        appname = (TextView)findViewById(R.id.appname);
        Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
        appname.setTypeface(tf);

        setUpDrawer();
    }

    private void setUpDrawer() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
        mDrawerLayout.setDrawerListener(mDrawerListener);
        expListView = (ExpandableListView) findViewById(R.id.lvExp);
        prepareListData();
        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        expListView.setAdapter(listAdapter);
        fragment = new Home();
        getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
        mDrawerLayout.closeDrawer(expListView);

        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                                @Override
                                public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                                    switch (groupPosition) {
                                        case 0:
                                            switch (childPosition) {
                                                case 0:
                                                    fragment = new 1();
                                                    break;
                                                case 1:
                                                    fragment = new 2();
                                                    break;
                                                case 2:
                                                    fragment = new 3();
                                                    break;
                            ...
                        }
                        break;

                    case 1:
                        switch (childPosition) {
                            case 0:
                                fragment = new 4();
                                break;
                           ...
                            default:
                                break;
                        }
                        break;

                    ...
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
                mDrawerLayout.closeDrawer(expListView);
                return false;
            }
        });

    }...

Upvotes: 0

Views: 62

Answers (1)

Shadab Ansari
Shadab Ansari

Reputation: 7070

On device rotation, activity is restarted - this is the apparent behavior. If you don't want your activity to get recreated, you should add android:configChanges="orientation" in your Manifest file for that activity.

Otherwise you can prevent calling setUpDrawer() when your activity gets restarted by checking savedInstanceState like this -

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String fontPath = "neue.ttf";
        setContentView(R.layout.activity_main);
        home = (ImageView)findViewById(R.id.home);
        home.setOnClickListener(homeOnclickListener);
        appname = (TextView)findViewById(R.id.appname);
        Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
        appname.setTypeface(tf);

       if(savedInstanceState == null){
           setUpDrawer();
      }
    }

Upvotes: 1

Related Questions