使用后退按钮在片段之间导航

时间:2018-02-22 16:39:16

标签: java android android-fragments

我知道这个问题已经讨论过了,但我在片段之间实现导航时遇到了困难。我想使用后退按钮返回上一个片段或主要活动。现在,后退按钮仅关闭导航抽屉或退出应用程序。我知道我应该使用addToBackStack,但我不确切知道如何。

我的主要活动:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setTitle("infoGmina");

    ImageView gminaImageView = (ImageView) findViewById(R.id.gminaImageView);
    ImageView weatherImageView = (ImageView) findViewById(R.id.weatherImageView);
    ImageView partlyCloudImageView = (ImageView) findViewById(R.id.partlyCloudImageView);

    partlyCloudImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            PogodaFragment fragment = new PogodaFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.replacableConstraintLayout, fragment);
            transaction.commit();

        }
    });

    weatherImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            WeatherFragment fragment = new WeatherFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.replacableConstraintLayout, fragment);
            transaction.commit();

        }
    });

    gminaImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AnnouncementListFragment fragment = new AnnouncementListFragment();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.replacableConstraintLayout, fragment);
            transaction.commit();

        }
    });


    ParseAnalytics.trackAppOpenedInBackground(getIntent());

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_gmina) {
        AnnouncementListFragment listFragment = new AnnouncementListFragment();
        FragmentManager manager = getFragmentManager();
        manager.beginTransaction().replace(R.id.replacableConstraintLayout, listFragment).commit();
        setTitle("Gmina");
    } else if (id == R.id.nav_restauracja) {

    } else if (id == R.id.nav_sklep) {

    } else if (id == R.id.nav_weather) {

        WeatherFragment weatherFragment = new WeatherFragment();
        FragmentManager manager = getFragmentManager();
        manager.beginTransaction().replace(R.id.replacableConstraintLayout, weatherFragment).commit();
        setTitle("Jakość powietrza");

    } else if (id == R.id.nav_pogoda) {

        PogodaFragment pogodaFragment = new PogodaFragment();
        FragmentManager manager = getFragmentManager();
        manager.beginTransaction().replace(R.id.replacableConstraintLayout, pogodaFragment).commit();
        setTitle("Pogoda");

    } else if (id == R.id.nav_onas) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

0 个答案:

没有答案