Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back Stack maintain Without Recreating Fragment #74

Closed
ayaz56 opened this issue Sep 15, 2021 · 6 comments
Closed

Back Stack maintain Without Recreating Fragment #74

ayaz56 opened this issue Sep 15, 2021 · 6 comments
Labels
wontfix Proposed changes won't be applied

Comments

@ayaz56
Copy link

ayaz56 commented Sep 15, 2021

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
Copy link
Owner

st235 commented Sep 18, 2021

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 st235 added good first issue Good for newcomers help wanted Extra attention is needed labels Sep 18, 2021
@st235
Copy link
Owner

st235 commented Sep 21, 2021

Hi @anubhav217 👋
Are you using this approach?

Best wishes,
Alex

@ayaz56
Copy link
Author

ayaz56 commented Sep 21, 2021 via email

@st235
Copy link
Owner

st235 commented Sep 22, 2021

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

@ayaz56
Copy link
Author

ayaz56 commented Sep 23, 2021 via email

@st235
Copy link
Owner

st235 commented Oct 2, 2021

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

@st235 st235 added wontfix Proposed changes won't be applied and removed help wanted Extra attention is needed good first issue Good for newcomers labels Oct 2, 2021
@st235 st235 closed this as completed Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix Proposed changes won't be applied
Projects
None yet
Development

No branches or pull requests

2 participants