Skip to content

Instantly share code, notes, and snippets.

@anton46
Created February 23, 2015 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anton46/425b3f1d0b2cdafb7e54 to your computer and use it in GitHub Desktop.
Save anton46/425b3f1d0b2cdafb7e54 to your computer and use it in GitHub Desktop.
public class MainActivity extends ActionBarActivity {
int number = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
if(getSupportFragmentManager().getBackStackEntryCount() == 0) finish();
}
});
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(1))
.addToBackStack(null)
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_add) {
addFragment(++number);
return true;
} else if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void addFragment(int number) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, PlaceholderFragment.newInstance(number))
.addToBackStack(null)
.commit();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public static final String ARG_NUMBER = "number";
private int number;
public static PlaceholderFragment newInstance(int number) {
Bundle b = new Bundle();
b.putInt(ARG_NUMBER, number);
PlaceholderFragment fragment = new PlaceholderFragment();
fragment.setArguments(b);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
number = getArguments().getInt(ARG_NUMBER, 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.num_of_fragment);
textView.setText("Fragment - " + number);
return rootView;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment