0

So I stole this little guide from another question on here...

sliding drawer appear in all activities

I have it all setup, and it works on ONLY the very first activity I put it in.

This is my BodyActivity, which is the default, homepage of the app.

public class BodyActivity extends DrawerActivity {

    private static final String TAG = "BodyActivity";

    private String email = "null";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body);





    }
    [...]
}

This is my DrawerActivity

public class DrawerActivity extends Activity
      implements NavigationView.OnNavigationItemSelectedListener {

    private static final String TAG = "DrawerActivity";

    protected RelativeLayout fullLayout;
    protected FrameLayout frameLayout;


    private static ActionBarDrawerToggle toggle = null;
    private static DrawerLayout drawer = null;
    private static NavigationView navigationView = null;

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_drawer, null);
            frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);

        updatePanel();


        //Intent thisIntent = getIntent();
        //updateText(navigationView, thisIntent);
    }

    private boolean updatePanel() {

        for (int i = 1; i <= 5; i++) {
            try {
                Log.d(TAG, "Tryin to update toolbar panel... Try #"+i);
                drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

                if (toolbar == null) {
                    Log.d(TAG, "toolbar null");
                    //return;
                }

                toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                //drawer.setDrawerTitle(Gravity.LEFT, "Open Navigation Pane");
                //drawer.setDrawerListener(toggle);
                //drawer.setDrawerListener(toggle);

                navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);


                drawer.post(new Runnable() {

                    @Override
                    public void run() {

                        toggle.syncState();

                    }

                });

                return true;
            } catch (Exception e) {
                e.printStackTrace();

            }

        }
        return true;
    }
}

What doesn't make sense to me, is the fact that my other Activity is set up the EXACT same as the BodyActivity. The class extends DrawerActivity. The ONLY difference, is it does a little bit of logic in the onCreate method.

The for-loop in the updatePanel method was a test.. didn't do anything helpful. [hr]

I ran a debugger that had a breakpoint on each of the three variables in the drawer create method.

1.) drawer 2.) toggle 3.) navigationView

On the very first run, which leads to the BodyActivity, all three variables have values set to them. I go through, and select a value in the drawer, that takes me to another activity, that ALSO has the drawer class extended, and when the debugger runs, all three are null.

ALSO on a sidenote, I have tried making all the variables static, not static, I've tried to make them local, all of which do not work.

EDIT: Per request, I added what is actually giving the error..

CalendarActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calendar);

    HashSet<Date> events = new HashSet<>();
    //TODO Add events
    //events.add(new Date());

    CalendarView cv = ((CalendarView)findViewById(R.id.calendar_view));
    cv.updateCalendar(events);

    // assign event handler
    cv.setEventHandler(new CalendarView.EventHandler()
    {
        @Override
        public void onDayLongPress(Date date)
        {
            // show returned day
            DateFormat df = SimpleDateFormat.getDateInstance();
            Toast.makeText(CalendarActivity.this, df.format(date), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDayPress(Date date) {

        }
    });

activity_calendar.xml - Custom CalendarView, if that means anything to you guys

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_marginTop="42dp"
tools:context=".calendar.CalendarActivity">

<com.endlesssolutions.classcommunications.calendar.CalendarView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/calendar_view"/>

</LinearLayout>

Literally that, the imports, the class title and Extends DrawerActivity are the only things of relavence in this class. They are set up the exact same, which is what doesn't make sense. Also, the activity layout shouldn't have anything to do with it in my eyes, because either way, DrawerActivity is finding the EXACT same activity, because its the same onCreate... nothing is changed (This is what doesn't make sense to me). And according to the debugger, it breaks on...

setContentView(R.layout.activity_calendar);

but I'm not sure why..


So... I figured it out. I'm honestly not entirely sure why.. so if someone would like the explain to me why this made it work, I'd appreciate it...

I originally had the DrawerActivity in the App Manifest.. I was jus putsing around, and removed it, and not everything works again..

Community
  • 1
  • 1
demitchell14
  • 195
  • 1
  • 1
  • 10

1 Answers1

0

this is not a answer(just a hint) but you can try to use sharedPreferences to store a flag and check it everytime onCreate is called. if flag=0 then put the drawerLayout

after the activity destroys make the flag=1 and store it in sharedPreference and the next time you come check for the flag now the flag=1 so dont put the drawerlayout

  • well, I tried to do something similar, and made a static boolean, and if it was activated, then it doesn't have to mess with that.. but it doesn't work, since my next screen ( activity) also extends DrawerActivity, it would create a new DrawerActivity class, then everything would need to be redeclared.. - at least that's how this whole thing works in my head.. – demitchell14 Jul 25 '16 at 22:24
  • maybe i didn't get you,but you can use PreferenceManager.getDefaultSharedPreferences() to get a unique SharedPreference for your application ,May be that can solve your issue – Manojit Paul Jul 25 '16 at 22:34