Search code examples
javaandroidandroid-fragmentsandroid-intentandroid-fragmentactivity

Second activity won't go back to first


I ran into a small problem. I am building an app for my android development course and I have two activities - the MainActivity and a fragments activity with three fragment layouts. I managed to write the code for when you click a button in the MainActivity and it takes you to the second fragment activity. This works great but when I click the back button it closes the app instead of returning to the primary activity layout. Here's some code:

MainActivity:

public class MainActivity extends AppCompatActivity {


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

    ButterKnife.bind(this);
    getSecondAct();

}

public void getSecondAct(){
    Button btnstart = (Button) findViewById(R.id.btnfragments);
    btnstart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent1 = new Intent(MainActivity.this, 
FragmentsActivity.class);
            startActivity(intent1);
        }
    });
}

@Override
protected void onStop() {
    super.onStop();
    Toast.makeText(this, "Thank you for using this app! :)", 
    Toast.LENGTH_SHORT).show();
    finish();
    } 
}

FragmentsActivity:

public class FragmentsActivity extends AppCompatActivity {

private FragmentsAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new FragmentsAdapter(getSupportFragmentManager());


    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_fragments, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

FragmentsAdapter:

public class FragmentsAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {
    if (position == 0) {    //position ide od nule!
        return RollDiceFragment.newInstance();
    } else if (position == 1) {
        return FAEFragment.newInstance();
    } else if (position == 2) {
        return FateCoreFragment.newInstance();
    } else {
        return null;
    }
}

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

The fragments themselves don't have any specific code in them yet, because I haven't gotten to the layout yet. I am using a tabbed activity to switch between the three fragments and I have no ide why the app closes instead of going back to the MainActivity screen.


Solution

  • you are calling finish() on your onStop method which is caused to destroy your launcher activity.remove that code and you are good to go.