package com.example.harrix.myapplication; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new OneFragment(), "ONE"); adapter.addFragment(new TwoFragment(), "TWO"); adapter.addFragment(new ThreeFragment(), "THREE"); viewPager.setAdapter(adapter); } class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } } 

 package com.example.harrix.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.widget.ImageView; import com.squareup.picasso.Picasso; public class haqqimiz extends AppCompatActivity { Toolbar toolbar; ImageView haqiimizimage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.haqqimiz); // toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true); picasso .load("https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/10686674_1579500448958894_5264196578205990291_n.jpg?oh=a33cdf0109f2032f19049d594f70b47f&oe=58203FFB") .into(haqiimizimage); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } } 

How to close 2 Activations and get to MainAcitvity using the Toolbar I did not succeed

  • one
    You can, for example, run MainAcitvity with back-up cleaning. - Vladyslav Matviienko
  • I've already done everything - elik
  • just put the finish) killed activity, as it were - elik
  • @elik if you find the answer - write it here, maybe it will help others in the future - Silento

2 answers 2

The code of the worker in it is no problem there was a problem in the emulator)))) if anyone needs to take

     Intent intent = new Intent(SecondActivity.this, MainActivity.class ); SecondActivity.this.finish(); startActivity(intent); 

    Switches to the MainActivity and calls the onDestroy () method of the SecondActivity.

    • Why do you need an intention? There is no sense you give him the despondency of the transition, it will work for the 1st for 2x and 15, but what if the project is at 40-45 class fragments? The incorrect method to destroy by intention - elik
    • I do not understand your comment: incorrect method to destroy through intent - Adeptius
    • You give a clear intent through the intent: it is not well understood? The life cycle is overlaid by the table one on the other and you close the final activating bypassing everything before that, in fact you have activi in ​​your memory, or in short there are 10 pages you went from the first to 10 but closing 10 opened the other 9 - elik
    • both use the posuti Death activate but yours is not death but more transition with closure - elik
    • one
      BUT! I mean that I will erase the backStackActivity with an intent? Well yes. The question is, after all, it was not “how to return to MainAcitvity”, but “to get on MainAcitvity”. Here, with the help of the intent, you can get there, and the fact that the backstage will be erased - I did not think that it matters to you. - Adeptius