Skip to content

Instantly share code, notes, and snippets.

@MartinRGB
Last active September 11, 2017 11:29
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 MartinRGB/1b8fe288d6447e921aaac1d8b11d74d3 to your computer and use it in GitHub Desktop.
Save MartinRGB/1b8fe288d6447e921aaac1d8b11d74d3 to your computer and use it in GitHub Desktop.
Tempalte of Fragment Creating
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import com.zhy.autolayout.AutoLayoutActivity;
/**
* Created by MartinRGB on 2017/8/31.
*/
public abstract class SingleFragmentActivity extends AutoLayoutActivity{
//Abstarct Mehotd
protected abstract Fragment createFragment();
//Life Cycle
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
deleteBars();
setContentView(R.layout.activity_main);
addFragment();
}
//Add Fragment
private void addFragment(){
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_container);
if(fragment == null){
fragment = createFragment();
fragmentManager.beginTransaction()
.add(R.id.fragment_container,fragment)
.commit();
}
}
////################### Delete ###################
private void deleteBars(){
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
////################### Onfocus Nav Bar ###################
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
public class MainActivity extends SingleFragmentActivity{
private MultiTaskFragment thisFragment;
@Override
public void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
}
@Override
protected android.support.v4.app.Fragment createFragment() {
thisFragment = MultiTaskFragment.newInstance();
return thisFragment;
}
//################### Key Utils ###################
public void onBackPressed(){
Intent intent = getIntent();
finish();
startActivity(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ) {
thisFragment.myOnKeyDown(keyCode);
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP ) {
thisFragment.myOnKeyDown(keyCode);
}
return super.onKeyDown(keyCode, event);
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by MartinRGB on 2017/8/31.
*/
public class CustomFragment extends Fragment {
private static final String TAG = "MultiTaskFragment";
public static CustomFragment newInstance() {
return new CustomFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
View view = inflater.inflate(R.layout.fragment_container, container, false);
return view;
}
@Override
public void onCreate(Bundle savedInstancestate) {
super.onCreate(savedInstancestate);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
public void myOnKeyDown(int key_code) {
//do whatever you want here
if ((key_code == KeyEvent.KEYCODE_VOLUME_DOWN)) {
Log.e(TAG,"KEYCODE_VOLUME_DOWN");
}
if ((key_code == KeyEvent.KEYCODE_VOLUME_UP)) {
Log.e(TAG,"KEYCODE_VOLUME_UP");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment