st235 / ExpandableBottomBar

A new way to implement navigation in your app 🏎
MIT License
783 stars 58 forks source link

Back Stack maintain Without Recreating Fragment #74

Closed ayaz56 closed 3 years ago

ayaz56 commented 3 years ago

package ng.com.obkm.bottomnavviewwithfragments;

import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast;

import ng.com.obkm.bottomnavviewwithfragments.home.HomeFragment;

public class MainActivity extends AppCompatActivity {

boolean doubleBackToExitPressedOnce = false;
final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new DashboardFragment();
final Fragment fragment3 = new NotificationsFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
BottomNavigationView navigation;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    setFragment(fragment1, "1", 0);
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                setFragment(fragment1, "1", 0);
                return true;
            case R.id.navigation_dashboard:
                setFragment(fragment2, "2", 1);
                return true;
            case R.id.navigation_notifications:
                setFragment(fragment3, "3", 2);
                return true;
        }
        return false;
    }
};

public void setFragment(Fragment fragment, String tag, int position) {
    if (fragment.isAdded()) {
        fm.beginTransaction().hide(active).show(fragment).commit();
    } else {
        fm.beginTransaction().add(R.id.main_container, fragment, tag).commit();
    }
    navigation.getMenu().getItem(position).setChecked(true);
    active = fragment;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        startActivity(new Intent(MainActivity.this, SettingsActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    if (active == fragment1) {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
    } else {
        setFragment(fragment1, "1", 0);
    }
}

}

st235 commented 3 years ago

Hello @ayaz56 πŸ‘‹

Sorry for a little bit late reply.

Thank you very much for using this library. Have you tried to use this approach?

Best wishes, Alex

st235 commented 3 years ago

Hi @anubhav217 πŸ‘‹ Are you using this approach?

Best wishes, Alex

ayaz56 commented 3 years ago

Yes

On Tue, 21 Sep, 2021, 1:28 PM Alexander Dadukin, @.***> wrote:

Hi @anubhav217 https://github.com/anubhav217 πŸ‘‹ Are you using this approach https://github.com/st235/ExpandableBottomBar/blob/master/app/src/main/java/github/com/st235/expandablebottombar/screens/navigation/NavigationComponentActivity.kt#L22 ?

Best wishes, Alex

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/st235/ExpandableBottomBar/issues/74#issuecomment-923733736, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALVX6KUCAJR3ZDROFZEKI23UDA3LVANCNFSM5ECJREZQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

st235 commented 3 years ago

Thank you for reply, will take a look at the problem in a few days

ayaz56 commented 3 years ago

Ok, thanks.

On Thu, 23 Sep, 2021, 12:22 AM Alexander Dadukin, @.***> wrote:

Thank you for reply, will take a look at the problem in a few days

β€” You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/st235/ExpandableBottomBar/issues/74#issuecomment-925208310, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALVX6KQTTXSW2JKPZVSPOJDUDIQW7ANCNFSM5ECJREZQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

st235 commented 3 years ago

Hello folks πŸ‘‹ Sorry for the late reply.

After careful consideration, I can say that the NavigationUI class from the navigation library behaves the same way. I believe this question is out of the genuine library scope and should be solved within your navigation component.

Best wishes, Alex