michnovka
michnovka

Reputation: 3389

Use navigation drawer with activities or fragments?

I have Android app with sidebar nav drawer layout and I am implementing functionality of a simple SMS app.

My question is, how to reuse nav drawer code between activities. Every example uses Fragments that are displayed in some master view after item in nav drawer menu is clicked. What if I launch new activity and want to have the same side menu as the original one?

Is there an official suggestion by Google how to implement this?

The problem for me is that in order to be default SMS app on Android, you must have some special activities handling certain Intents.

Should I dump Activities fully and implement everything using Fragments?

Thanks

Upvotes: 0

Views: 1154

Answers (3)

Yash Sampat
Yash Sampat

Reputation: 30601

Google's Android apps have a specific architecture which consists of the following:

  • There is often just a single Activity in the entire app, and if more activities are needed, then they all extend the same BaseActivity.
  • Different screens are represented as individual Fragments. Screen transitions take place through fragment transactions all within the same BaseActivity.

For an example of this, look no further than the source code of the Google I/O app:

The way to integrate the drawer with different screens is illustrated in this project, and further elaborated in the following articles:

0. Guide to App Architecture

1. Android App Structure.

2. Planning Screens and Their Relationships.

3. Providing Descendant and Lateral Navigation.

4. Providing Ancestral and Temporal Navigation.

5. Patterns – Navigation.

6. Best Practices for User Interface.


More examples of apps that employ these (and other) guidelines:

Upvotes: 5

roopesh
roopesh

Reputation: 1

public class Tab2Fragment extends Fragment {

public int currentimageindex=0;
//  Timer timer;

// TimerTask task; ImageView slidingimage;

private int[] IMAGE_IDS = {
        R.drawable.splash0, R.drawable.splash1, R.drawable.splash2,
        R.drawable.splash3, R.drawable.splash4, R.drawable.splash5
};


@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.tab2fragment, container, false);


    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();

        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 5000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

            mHandler.post(mUpdateResults);

        }

    }, delay, period);




    return v;
}





/**
 * Helper method to start the animation on the splash screen
 */
private void AnimateandSlideShow() {


    slidingimage = (ImageView) slidingimage.findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

    currentimageindex++;

    Animation rotateimage = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);


    slidingimage.startAnimation(rotateimage);



}

Upvotes: 0

piotrpo
piotrpo

Reputation: 12636

This is little to broad question so let's focus on reusing drawer. Content of the drawer is nothing more than a view, so probably best way to have it reusable is to create custom view by extending FrameLayout (or something else that is suitable for your particular design. In very basic form all you have to do is something like (init block syntax below):

DrawerView extends FrameLayout
{
  {
    LayoutInflater.from(getContext()).inflate(R.layout.my_drawer_layout, this, true);
  }
}

Of course you probably want to put rest of the logic in additional methods or in the init block itself (for example handling clicks, initializing adapters etc.) Then simply build this code and you are ready to use your new custom control in every place you want.

Upvotes: 0

Related Questions