0

My app uses a Navigation Drawer on only some activities. Instead of duplicating the code in each activity, I want to modify the base activity they extend so the navigation drawer code is only in one place. The problem is that the onItemClick method will vary depending on the activity. For instance, pressing "Home" in MainActivity should do nothing, since we're already at "Home." Pressing "Home" in ListActivity should take me to MainActivity.

(Please see my answer to my own question here for a little more information. I am finishing each activity when the user navigates away from it, and passing an extra to inform the next activity what the calling activity was. This was done to avoid the animation of the drawer closing when the user returns via the device back button. This way, when they return, the drawer is fully closed without animation because the activity has been restarted.)

What can I do to get the necessary information to the superclass? I would need each of the subclasses to pass some sort of information to the superclass. Maybe each subclass could pass an int which identifies itself to the superclass, and the superclass can use that int to determine what to do in each case? I'm not sure how to do that, if it is possible.

Possible superclass BaseActivity implementation:

mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id)
    {
        //passedParameter is an int that the subclass passes to the superclass to identify itself
        switch(int passedParameter)
        {
            //int passed from subclass identifies it as MainActivity
            case MAIN_ACTIVITY:
            {
                switch(position)
                {
                    //HOME
                    case 0:
                        //do nothing since we're already at MainActivity/home
                    //LIST
                    case 1:
                        //go to ListActivity
                }
            }
            //int passed from subclass identifies it as ListActivity
            case LIST_ACTIVITY:
            {
                switch(position)
                {
                    //HOME
                    case 0:
                        //go to MainActivity
                    //LIST
                    case 1:
                        //do nothing since we're already at ListActivity
                }
            }
        }
    }
});

How could I pass the necessary int from the subclass to the superclass?

BaseActivity is abstract:

public abstract class BaseActivity extends FragmentActivity
{
    protected abstract Fragment createFragment();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        //navigation drawer code shown above, plus more

        if (fragment == null)
        {
            fragment = createFragment();
            fm.beginTransaction()
                    .add(R.id.fragmentContainer, fragment)
                    .commit();
        }
    }
}

And the subclass will have only this implementation:

public class MainActivity extends BaseActivity
{
    //how to pass some parameter to BaseActivity from here?
    @Override
    protected Fragment createFragment()
    {
        return new MainFragment();
    }
}

Please let me know if I am unclear. I appreciate any help. Thanks!

Community
  • 1
  • 1
pez
  • 3,859
  • 12
  • 40
  • 72

2 Answers2

1

I would need each of the subclasses to pass some sort of information to the superclass to determine the implementation of onItemClick

You can create a protected variable to be used in the subclass.

Example in superclass :

protected boolean isLogin = false;

Then you can set the value of isLogin in the subclass's onItemClick.

UPDATE

It seems you need to know what class extended the superclass by using instance of. Example :

if(this instance of MainActivity.class)
Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • Thanks for the reply. The subclass wouldn't be implementing `onItemClick`, though- that code would be called in the superclass only so I don't have to duplicate it in more than one class. Could I pass the information through another method? – pez Nov 11 '14 at 04:17
  • What information are you trying to pass? I think using protected variable (or method) are the best way – Blaze Tama Nov 11 '14 at 04:19
  • I think just passing a simple int would be good enough. For example, pass 0 for `MainActivity` and 1 for `ListActivity`. Then in the `onItemClick` method of the superclass, I can switch on the parameter passed within the first switch statement to determine what to do. I'll edit my question – pez Nov 11 '14 at 04:24
  • @pez i still dont get what you want, but i will update my answer :) – Blaze Tama Nov 11 '14 at 04:27
  • `if(this instance of MainActivity.class)` seems like it would work! I will quickly try to implement it and update. – pez Nov 11 '14 at 04:33
  • @pez Okay :) Sorry for my English, not my foreign language – Blaze Tama Nov 11 '14 at 04:34
  • I don't think that will work, since I would be calling it in the superclass, which is not an instance of any of the subclasses – pez Nov 11 '14 at 04:43
0

A way of passing information from subclass to superclass:

In superclass:

public abstract class BaseActivity extends FragmentActivity
{
    protected abstract int sendSubActivity();
    ...
}

In subclass:

public class MainActivity extends BaseActivity
{
    @Override
    protected int sendSubActivity()
    {
        return CallingActivityInterface.MAIN_ACTIVITY;
    }
    ...
}
pez
  • 3,859
  • 12
  • 40
  • 72