package de.markusfisch.android.shadereditor.activity;

import de.markusfisch.android.shadereditor.app.ShaderEditorApplication;
import de.markusfisch.android.shadereditor.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public abstract class AbstractSubsequentActivity extends AppCompatActivity {
	private FragmentManager fm;

	public static void addFragment(
			FragmentManager fm,
			Fragment fragment) {
		fm.beginTransaction()
				.replace(
						R.id.content_frame,
						fragment)
				.addToBackStack(null)
				.commit();
	}

	public static void setFragment(
			FragmentManager fm,
			Fragment fragment) {
		fm.beginTransaction()
				.replace(
						R.id.content_frame,
						fragment)
				.commit();
	}

	public static void initToolbar(AppCompatActivity activity) {
		Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar);
		activity.setSupportActionBar(toolbar);
		activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	public static void initSystemBars(AppCompatActivity activity) {
		MainActivity.setSystemBarColor(
				activity.getWindow(),
				ShaderEditorApplication
						.preferences
						.getSystemBarColor(),
				false);
	}

	@Override
	public boolean onSupportNavigateUp() {
		if (fm.getBackStackEntryCount() > 0) {
			fm.popBackStack();
		} else {
			finish();
		}

		return true;
	}

	@Override
	protected void onCreate(Bundle state) {
		super.onCreate(state);
		fm = getSupportFragmentManager();
	}

	protected void setFragmentForIntent(
			Fragment fragment,
			Intent intent) {
		if (fragment == null || intent == null) {
			finish();
			return;
		}

		fragment.setArguments(intent.getExtras());
		setFragment(getSupportFragmentManager(), fragment);
	}
}

@@ -167,16 +167,56 @@
 tor.R;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -234,24 +234,24 @@
 ent.Intent;%0A
-
 import andro
@@ -351,16 +351,67 @@
 anager;%0A
+import android.support.v4.app.FragmentTransaction;%0A
 import a
@@ -687,76 +687,42 @@
 %7B%0A%09%09
-fm.beginTransaction()%0A%09%09%09%09.replace(%0A%09%09%09%09%09%09R.id.content_frame,%0A%09%09%09%09%09%09
+getReplaceFragmentTransaction(fm, 
 frag
@@ -858,76 +858,42 @@
 %7B%0A%09%09
-fm.beginTransaction()%0A%09%09%09%09.replace(%0A%09%09%09%09%09%09R.id.content_frame,%0A%09%09%09%09%09%09
+getReplaceFragmentTransaction(fm, 
 frag
@@ -897,21 +897,16 @@
 ragment)
-%0A%09%09%09%09
 .commit(
@@ -1901,11 +1901,235 @@
 ent);%0A%09%7D
+%0A%0A%09@SuppressLint(%22CommitTransaction%22)%0A%09private static FragmentTransaction getReplaceFragmentTransaction(%0A%09%09%09FragmentManager fm,%0A%09%09%09Fragment fragment) %7B%0A%09%09return fm.beginTransaction().replace(R.id.content_frame, fragment);%0A%09%7D
 %0A%7D%0A

Replace fragment in just one place