Last active
November 15, 2017 10:13
-
-
Save philandrews100/70e19b0ca98df28da8333cbd58c7485a to your computer and use it in GitHub Desktop.
Fragment Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Fragment; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.retrofitrealm.controllers.MainActivityInterface; | |
import butterknife.ButterKnife; | |
import butterknife.Unbinder; | |
/** | |
* Created by Phil on 10/01/2017. | |
*/ | |
public abstract class BaseFragment extends Fragment { | |
private Unbinder baseUnbinder; | |
public MainActivityInterface mainActivityInterface; | |
public abstract int getLayout(); | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(getLayout(), container, false); | |
baseUnbinder = ButterKnife.bind(this, view); | |
return view; | |
} | |
@Override | |
public void onDestroyView() { | |
super.onDestroyView(); | |
if(null != baseUnbinder) | |
baseUnbinder.unbind(); | |
} | |
@Override | |
public void onAttach(Context context) { | |
super.onAttach(context); | |
mainActivityInterface = (MainActivityInterface) context; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
import com.retrofitrealm.R; | |
import com.retrofitrealm.fragments.assets.BaseFragment; | |
import butterknife.BindView; | |
/** | |
* Created by Phil on 10/01/2017. | |
*/ | |
public class ImagesFragment extends BaseFragment { | |
private static String TV_VALUE = "tvValue"; | |
@BindView(R.id.tvTitle) | |
TextView tvTitle; | |
public static BaseFragment newInstance(String tvValue) { | |
ImagesFragment imagesFragment = new ImagesFragment(); | |
Bundle arg0 = new Bundle(); | |
arg0.putString(TV_VALUE, tvValue); | |
imagesFragment.setArguments(arg0); | |
return imagesFragment; | |
} | |
@Override | |
public int getLayout() { | |
return R.layout.images_fragment; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = super.onCreateView(inflater, container, savedInstanceState); | |
tvTitle.setText(getArguments().getString(TV_VALUE)); | |
return view; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.FragmentManager; | |
import android.app.FragmentTransaction; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.retrofitrealm.R; | |
import com.retrofitrealm.controllers.MainActivityInterface; | |
import com.retrofitrealm.fragments.ImagesFragment; | |
import com.retrofitrealm.fragments.assets.BaseFragment; | |
public class MainActivity extends AppCompatActivity implements MainActivityInterface { | |
private FragmentManager fragManager; | |
private FragmentTransaction fragTransaction; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
setupFragmentManager(); | |
loadImagesFragment("This is a fragment"); | |
} | |
@Override | |
public void loadImagesFragment(String tvValue) { | |
switchFragment(ImagesFragment.newInstance(tvValue), "Images Fragment"); | |
} | |
@Override | |
public void goBack() { | |
onBackPressed(); | |
} | |
@Override | |
public void onBackPressed() { | |
if (this.fragManager.getBackStackEntryCount() != 1) { | |
fragManager.popBackStack(); | |
if (this.fragManager.getBackStackEntryCount() == 0) { | |
super.onBackPressed(); | |
} | |
} else { | |
finish(); | |
} | |
} | |
private void switchFragment(BaseFragment fragment, String fragmentName) { | |
this.fragTransaction = getBaseFragmentManager().beginTransaction(); | |
this.fragTransaction.replace(R.id.flMainContainer, fragment, fragmentName); | |
this.fragTransaction.addToBackStack(fragmentName); | |
this.fragTransaction.commit(); | |
} | |
private FragmentManager getBaseFragmentManager() { | |
if (null == this.fragManager) { | |
throw new IllegalStateException(MainActivity.class.getSimpleName() + | |
" is not initialized, call setupFragmentManager(...) first"); | |
} | |
return this.fragManager; | |
} | |
private void setupFragmentManager() { | |
fragManager = getFragmentManager(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Phil on 10/01/2017. | |
*/ | |
public interface MainActivityInterface { | |
void loadImagesFragment(String tvValue); | |
void goBack(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment