0

В Activity2 есть bottom navigation view с пятью вкладками, при нажатии на каждую из них открывается определенный fragment. При переходе из Activity1 в Activity2 открывается первая вкладка с fragment1. После чего открываем вторую вкладку с fragment2. Во fragment2 при нажатию на кнопку переходим в Activity3. По нажатию navigation icon в toolbar, который находится в Activity3, переходим обратно в Activity2 и открывается первая вкладка с fragment1, а нужно чтобы открывалась вторая вкладка с fragment2.

Activity1.class:

public class Activity1 extends AppCompatActivity implements View.OnClickListener {

    Button btnDone;

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

        btnDone = (Button) findViewById(R.id.btnDone);

        btnDone.setOnClickListener(this);
    }

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnDone:
                Intent newIntent = new Intent(this, Activity2.class);
                startActivity(newIntent);
       }
    }
}

Activity2.class:

public class Activity2 extends AppCompatActivity {

    private BottomNavigationView bottomNavigation;
    private Fragment fragment;
    private FragmentManager fragmentManager;

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

        bottomNavigation = (BottomNavigationView)findViewById(R.id.bottom_navigation);
        bottomNavigation.inflateMenu(R.menu.menu_nav);
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.main_container, new Fragment1())
                .commit();
        bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = item.getItemId();
                switch (id) {
                    case R.id.action_one:
                        fragment = new Fragment1();
                        break;
                    case R.id.action_two:
                        fragment = new Fragment2();
                        break;
                    case R.id.action_tree:
                        fragment = new Fragment3();
                        break;
                    case R.id.action_four:
                        fragment = new Fragment4();
                        break;
                    case R.id.action_five:
                        fragment = new Fragment5();
                        break;
                }
                final FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container, fragment).commit();
                return true;
            }
        });
    }

Fragment2.class:

public class Fragment2 extends Fragment {

Button btnNext;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);

        btnNext = (Button) view.findViewById(R.id.btn_next);
        btnNext.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                             startActivity(new Intent(this, Activity3.class));
                        }
                    });
        return view;
    }
}

Activity3.class:

public class Activity3 extends AppCompatActivity {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_leggins);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(this, Activity2.class);
                startActivity(intent);
            }
        });
    }
}

1 ответ 1

1

Думаю в вашем случае достаточно переписать setNavigationOnClickListener в Activity3 используя onBackPressed.

 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                    onBackPressed();

        }
    });

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.