Marcos Guimaraes
Marcos Guimaraes

Reputation: 1295

Make back button returns from an activity to a fragment in Android

I'm developing an Android application and I have a fragment called "More", in this fragment I have such things as "About Us", "Terms and Conditions", that are normal activities. What I want to do is, when I click on the back button inside the, for example, the about us Activity I want to return to the "More" fragment. How can I do that? This is what I have in the aboutUs activity:

The "onBackPressed method was supposed to go back to the fragment once I click on the back button, but it is not working. The application crashes and in the log cat is says "Have you declare the MoreFragment Activity in the manifest?", but I tried to declare and it doesn't work.

public class AboutUs extends BaseActivity {

    private ImageView image;

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

        image = (ImageView) findViewById(R.id.imageView2);

        image.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent launchactivity = new Intent(AboutUs.this, StartScreenActivity.class);

                startActivity(launchactivity);

            }

        });

    }

    @Override
    public void onBackPressed()
    {
        Intent intent = new Intent(this,MoreFragment.class);
        startActivity(intent);
    }
}

This is the fragment that calls the Activity:

public class MoreFragment extends Fragment implements View.OnClickListener {


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, null, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView lbl1 = (TextView) view.findViewById(R.id.lbl1);
        TextView lbl2 = (TextView) view.findViewById(R.id.lbl2);
        TextView lbl3 = (TextView) view.findViewById(R.id.lbl3);
        TextView lbl4 = (TextView) view.findViewById(R.id.lbl4);
        TextView lbl5 = (TextView) view.findViewById(R.id.lbl5);
        TextView lbl6 = (TextView) view.findViewById(R.id.lbl6);
        TextView lbl7 = (TextView) view.findViewById(R.id.lbl7);
        TextView lbl8 = (TextView) view.findViewById(R.id.lbl8);
        TextView lbl9 = (TextView) view.findViewById(R.id.lbl9);

        ImageView imv1 = (ImageView) view.findViewById(R.id.imv1);
        ImageView imv2 = (ImageView) view.findViewById(R.id.imv2);
        ImageView imv3 = (ImageView) view.findViewById(R.id.imv3);
        ImageView imv4 = (ImageView) view.findViewById(R.id.imv4);
        ImageView imv5 = (ImageView) view.findViewById(R.id.imv5);
        ImageView imv6 = (ImageView) view.findViewById(R.id.imv6);
        ImageView imv7 = (ImageView) view.findViewById(R.id.imv7);
        ImageView imv8 = (ImageView) view.findViewById(R.id.imv8);
        ImageView imv9 = (ImageView) view.findViewById(R.id.imv9);
        ImageView imv10 = (ImageView) view.findViewById(R.id.imv10);

        TextView btnSignOut = (TextView) view.findViewById(R.id.lbl10);

        lbl1.setOnClickListener(this);
        lbl2.setOnClickListener(this);
        lbl3.setOnClickListener(this);
        lbl4.setOnClickListener(this);
        lbl5.setOnClickListener(this);
        lbl6.setOnClickListener(this);
        lbl7.setOnClickListener(this);
        lbl8.setOnClickListener(this);
        lbl9.setOnClickListener(this);

        imv1.setOnClickListener(this);
        imv2.setOnClickListener(this);
        imv3.setOnClickListener(this);
        imv4.setOnClickListener(this);
        imv5.setOnClickListener(this);
        imv6.setOnClickListener(this);
        imv7.setOnClickListener(this);
        imv8.setOnClickListener(this);
        imv9.setOnClickListener(this);

        btnSignOut.setOnClickListener(this);

        View view9 = view.findViewById(R.id.view9);
        View view8 = view.findViewById(R.id.view8);

        if (!PrefernceHelper.getBoolean(getActivity(), Commons.Constants.USERLOGGEDIN)) {
            imv8.setVisibility(View.INVISIBLE);
            lbl8.setVisibility(View.INVISIBLE);
            imv10.setVisibility(View.INVISIBLE);
            btnSignOut.setVisibility(View.INVISIBLE);
            view9.setVisibility(View.INVISIBLE);
            view8.setVisibility(View.INVISIBLE);
        }

    }

    private void callAllMoreScreen(String type, Class<?> className) {

        Intent intent = new Intent();
        intent.setClass(getActivity(), className);
        intent.putExtra(Commons.Constants.LOAD_TYPE_URL, type);
        getActivity().startActivity(intent);

    }

    private void callAllMoreScreen1(Class<?> className) {

        Intent intent = new Intent();
        intent.setClass(getActivity(), className);
        getActivity().startActivity(intent);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.lbl1:
                callAllMoreScreen1(AboutUs.class);
                break;
            case R.id.lbl2:
                callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
                break;
            case R.id.lbl3:
                callAllMoreScreen1(TermsConditions.class);
                break;
            case R.id.lbl4:
                callAllMoreScreen1(PrivacyPolicy.class);
                break;
            case R.id.lbl5:
                callAllMoreScreen("faqs", AllMoreScreenActivity.class);
                break;
            case R.id.lbl6:
                callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
                break;
            case R.id.lbl7:
                callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
                break;
            case R.id.lbl8:
                callAllMoreScreen(" ", PredefinedAddressActivity.class);
                break;
            case R.id.lbl9:
                callAllMoreScreen(" ", TrackyourorderActivity.class);
                break;
            case R.id.imv1:
                callAllMoreScreen1(AboutUs.class);
                break;
            case R.id.imv2:
                callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
                break;
            case R.id.imv3:
                callAllMoreScreen1(TermsConditions.class);
                break;
            case R.id.imv4:
                callAllMoreScreen1(PrivacyPolicy.class);
                break;
            case R.id.imv5:
                callAllMoreScreen("faqs", AllMoreScreenActivity.class);
                break;
            case R.id.imv6:
                callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
                break;
            case R.id.imv7:
                callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
                break;
            case R.id.imv8:
                callAllMoreScreen(" ", PredefinedAddressActivity.class);
                break;
            case R.id.imv9:
                callAllMoreScreen(" ", TrackyourorderActivity.class);
                break;
            case R.id.lbl10:
                PrefernceHelper.clearAll(getActivity());
                callAllMoreScreen("", SignInActivity.class);
                getActivity().finish();
                break;
        }
    }
}

And this is the Activity that call the fragment:

public class TabLayoutScreenActivity extends BaseActivity {

    private TabLayout tabLayout;
    private Bundle bundle;
    //Bundle bundle;

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

        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); // actionBarSetUp(true);

        // getSupportActionBar().setIcon(R.drawable.small_bee);
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        // getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //  getSupportActionBar().setIcon(R.drawable.ic_launcher);
        viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
        ImageView foodlebee = (ImageView) findViewById(R.id.imageView2);

        foodlebee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent launchactivity = new Intent(TabLayoutScreenActivity.this,StartScreenActivity.class);
                startActivity(launchactivity);
            }

        });

        // bundle = getIntent().getExtras();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        if (PrefernceHelper.getBoolean(this, Commons.Constants.USERLOGGEDIN)) {
            MenuItem item = menu.findItem(R.id.user_login);
            item.setVisible(false);
            invalidateOptionsMenu();
        }
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.user_login) {
            intentCall(SignInActivity.class, null, 0);
            return true;
        } else if (item.getItemId() == R.id.home) {
            intentCall(StartScreenActivity.class, null, 0);
            finish();
            return true;
        }
        return false;

    }

    @Override
    protected void onStart() {
        super.onStart();
        bundle = getIntent().getExtras();
        int tabPostion = 0;
        if (bundle != null)
            tabPostion = bundle.getInt(Commons.Constants.TAB_POSITION, 0);

        TabLayout.Tab tab = tabLayout.getTabAt(tabPostion);
        tab.select();
    }

    public class SectionPagerAdapter extends FragmentPagerAdapter {

        public SectionPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new ChefFragment();
                case 1:
                    return new OrderFragment();
                case 2:
                    return new BasketFragment();
                case 3:
                    return new MoreFragment();
                default:
                    return new ChefFragment();
            }
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Chefs";
                case 1:
                    return "Orders";
                case 2:
                    return "Cart";
                case 3:
                    return "More";
                default:
                    return "Chef";
            }
        }
    }
}

Upvotes: 0

Views: 3213

Answers (1)

Arpit Ratan
Arpit Ratan

Reputation: 3026

Just finish() the current activity in onbackpressed hence you will return to the more fragment because the previous activity is already on the back stack. You don't need to launch the previous activity again.

Moreover your code is wrong.. you are trying to launch a fragment which is not possible. This is why this exception is thrown.

Change your code like this :

 @Override
    public void onBackPressed()
    {
        finish();
    }

Upvotes: 2

Related Questions