javaandroidslidingmenu

cant hide the Side Menu when hardware back button was clicked


Its is my Menu code

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Menu();
        init();
    }

public void init(){
    LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
    mRecomended.setOnClickListener(new OnClick());

    LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
    mLive.setOnClickListener(new OnClick());


    LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
    mLike.setOnClickListener(new OnClick());

    LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
    mFavorite.setOnClickListener(new OnClick());

    LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
    mHistory.setOnClickListener(new OnClick());

    LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
    mFeedback.setOnClickListener(new OnClick());

    LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
    mSettings.setOnClickListener(new OnClick());
}

private class OnClick implements View.OnClickListener {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        switch (v.getId()) {
            case R.id.recomended:
                intent.setClass(MainActivity.this,MainActivity.class);
                bundle.putString("theme", "Recomended");
                bundle.putString("From", "MainActivity");
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(0, 0);
                Log.e("theme","Recomended");
                break;
            case R.id.live:
                intent.setClass(MainActivity.this,LiveActivity.class);
                bundle.putString("theme", "Live");
                bundle.putString("From", "MainActivity");
                intent.putExtras(bundle);
                startActivity(intent);
                overridePendingTransition(0, 0);
                Log.e("theme","Live");
                break;
            default:
                break;
        }
    }
}
public void Menu(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //remove ToolBar Tittle
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // configure the SlidingMenu
    final SlidingMenu menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    // setting menu width
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.left_fragment);
    ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
    tool_menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menu.toggle();
        }
    });
    menu.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

}

public void Bundle(){
    Bundle getbundle =this.getIntent().getExtras();
    ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
    switch(getbundle.getString("theme")){
        case "Recomended":
            iRecomended.setVisibility(View.VISIBLE);
            break;
        case "Live":
            ImageView iLive = (ImageView)findViewById(R.id.live_line);
            iLive.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Like":
            ImageView iLike = (ImageView)findViewById(R.id.like_line);
            iLike.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Favorite":
            ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
            iFavorite.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "History":
            ImageView iHistory = (ImageView)findViewById(R.id.history_line);
            iHistory.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Feedback":
            ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
            iFeedback.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
        case "Settings":
            ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
            iSettings.setVisibility(View.VISIBLE);

            iRecomended.setVisibility(View.INVISIBLE);
            break;
    }

}}

and its my nextpage

public class LiveActivity extends AppCompatActivity {
    String[] name = {"1","1","1","1"};
    String[] nickname = {"MidnightSnack","MidnightSnack","MidnightSnack","MidnightSnack"};
    String[] date = {"March 1,2016","March 1,2016","March 1,2016","March 1,2016"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live_main);
        Menu();
        Bundle();
        RecycleView();
        init();
    }

    public void Bundle(){
        Bundle getbundle =this.getIntent().getExtras();
        ImageView iRecomended = (ImageView)findViewById(R.id.recomended_line);
        switch(getbundle.getString("theme")){
            case "Recomended":
                iRecomended.setVisibility(View.VISIBLE);
                break;
            case "Live":
                ImageView iLive = (ImageView)findViewById(R.id.live_line);
                iLive.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
            case "Like":
                ImageView iLike = (ImageView)findViewById(R.id.like_line);
                iLike.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
            case "Favorite":
                ImageView iFavorite = (ImageView)findViewById(R.id.favorite_line);
                iFavorite.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
            case "History":
                ImageView iHistory = (ImageView)findViewById(R.id.history_line);
                iHistory.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
            case "Feedback":
                ImageView iFeedback = (ImageView)findViewById(R.id.feedback_line);
                iFeedback.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
            case "Settings":
                ImageView iSettings = (ImageView)findViewById(R.id.settings_line);
                iSettings.setVisibility(View.VISIBLE);

                iRecomended.setVisibility(View.INVISIBLE);
                break;
        }

    }

    public void Menu(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //remove ToolBar Tittle
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        final SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        // setting menu width
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.left_fragment);
        ImageView tool_menu = (ImageView)toolbar.findViewById(R.id.menu);
        tool_menu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                menu.showMenu();
            }
        });
    }

    public void RecycleView(){
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        ArrayList<PostOfLiveActivity> data = new ArrayList<>();
        for(int i = 0; i < name.length; i++) {
            data.add(new PostOfLiveActivity(name[i], null, nickname[i],date[i]));
        }
        AdapterOfLiveActivity adapter = new AdapterOfLiveActivity(this, data);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

    }

    public void init(){
        LinearLayout mRecomended = (LinearLayout)findViewById(R.id.recomended);
        mRecomended.setOnClickListener(new OnClick());

        LinearLayout mLive = (LinearLayout)findViewById(R.id.live);
        mLive.setOnClickListener(new OnClick());

        LinearLayout mLike = (LinearLayout)findViewById(R.id.like);
        mLike.setOnClickListener(new OnClick());

        LinearLayout mFavorite = (LinearLayout)findViewById(R.id.favorite);
        mFavorite.setOnClickListener(new OnClick());

        LinearLayout mHistory = (LinearLayout)findViewById(R.id.history);
        mHistory.setOnClickListener(new OnClick());

        LinearLayout mFeedback = (LinearLayout)findViewById(R.id.feedback);
        mFeedback.setOnClickListener(new OnClick());

        LinearLayout mSettings = (LinearLayout)findViewById(R.id.settings);
        mSettings.setOnClickListener(new OnClick());
    }

    private class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            switch (v.getId()) {
                case R.id.recomended:
                    intent.setClass(LiveActivity.this,MainActivity.class);
                    bundle.putString("theme", "Recomended");
                    intent.putExtras(bundle);
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                    Log.e("theme","Recomended");
                    break;
                case R.id.live:
                    intent.setClass(LiveActivity.this,LiveActivity.class);
                    bundle.putString("theme", "Live");
                    intent.putExtras(bundle);
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                    Log.e("theme","Live");
                    break;
                default:
                    break;
            }
        }
    }
}

when i click hardware back button at LiveActivity , it back to MainActivity but my Slide Menu also opened , how can i do to hide them when I click hardware back button ??


Solution

  • If you happen to use https://github.com/jfeinstein10/SlidingMenu, i recommend you that you switch to NavigationDrawer instead, because that SlidingMenu is no longer maintained and has bugs that hasn't resolved yet. If you insist use that library, i recommend you to close the menu before go to the LiveActivity by calling

    if (menu.isMenuShowing()) {
        menu.showContent();
    }