//———————————————————–BaseActivity—————————————————————————–
public abstract class BaseActivity extends AppCompatActivity {
private FragmentManager mManager;
protected Unbinder mUnbinder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getActivityLayoutId());
mUnbinder= ButterKnife.bind(this);
MyApp.mContent=this;
MyApp.mList.add(this);
mManager=getSupportFragmentManager();
initView();
initData();
}
public void addFragments(int containerId, BaseFragment fragment) {
if (fragment != null)
Log.e("TAG222","addFragments===========执行了====");
mManager.beginTransaction().add(containerId, fragment, fragment.getClass().
getSimpleName()).addToBackStack(fragment.getClass().getSimpleName())
.commitAllowingStateLoss();
Log.e("TAG222","addFragments===========提交了====");
}
public void replaceFragment(int containerId, BaseFragment fragment) {
if (fragment != null)
mManager.beginTransaction().replace(containerId, fragment, fragment.getClass().
getSimpleName()).addToBackStack(fragment.getClass().getSimpleName())
.commitAllowingStateLoss();
}
@Override
protected void onDestroy() {
if (mUnbinder!=null){
mUnbinder.unbind();
}
super.onDestroy();
}
protected abstract void initData();
protected abstract void initView();
protected abstract int getActivityLayoutId();
}
//—————————————————————————-BaseFragment———————————————————–
public abstract class BaseFragment extends Fragment{
private Unbinder mUnbingder;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getFragmentLayoutId(), container, false);
mUnbingder= ButterKnife.bind(this,view);
initView();
initData();
return view;
}
protected abstract void initData();
protected abstract void initView();
protected abstract int getFragmentLayoutId();
@Override
public void onDestroy() {
if (mUnbingder!=null){
mUnbingder.unbind();
}
super.onDestroy();
}
}
//———————————————————————–App类————————————————————————–
public class MyApp extends Application{
public static BaseActivity mContent;
public static List<Activity>mList=new ArrayList<>();
@Override
public void onCreate() {
super.onCreate();
}
}
//———–所有的activity 都是继承的BaseActivity,所有的fragment都是继承的BaseFragment,App在AndroidManifest里调用
//例如:HomeActivity 继承 BaseActivity
public class HomeActivity extends BaseActivity{
private FragmentManager mManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//通过调用BaseActivity里的replaceFragment()方法开始在HomeActivity页面添加fragment
mManager=getSupportFragmentManager();
HomeFragment homeFragment = new HomeFragment();
replaceFragment(R.id.mID,homeFragment);
}
@Override
protected void initData() {
}
@Override
protected void initView() {
}
@Override
protected int getActivityLayoutId() {
MyApp application = (MyApp) getApplication();
application.mList.add(this);
return R.layout.activity_main;
}
//物理返回键,判断当前是不是最后一个Fragment,是就执行双击退出,不是一层一层退
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mManager != null) {
if (mManager.getBackStackEntryCount() > 1) {
mManager.popBackStack();
} else {
//双击退出
doubleCickFinish();
}
}
return true;
}
return super.onKeyDown(keyCode, event);
}
//双击退出
private boolean mIsExit;
private void doubleCickFinish() {
if (mIsExit == false) {
mIsExit = true;
ToastUtil.shoToast(HomeActivity.this, "再按一次退出程序");
new Timer().schedule(new TimerTask() {
@Override
public void run() {
mIsExit = false;
}
}, 2000);
} else {
// finish();
exitAllActivity();
}
}
//退出程序 关闭所有的Activity
private void exitAllActivity() {
if (MyApp.mList!=null){
for (int i = 0; i <MyApp.mList.size() ; i++) {
Activity activity = MyApp.mList.get(i);
activity.finish();
}
}
}
}
//———————————————-HomeFragment继承的BaseFragment———————————————————-
public class HomeFragment extends BaseFragment implements IView {
@BindView(R.id.mButton)
Button mButton;
@BindView(R.id.mTiao)
Button mTiao;
@BindView(R.id.mText1)
TextView mText1;
@Override
protected void initData() {
}
@Override
protected void initView() {
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Presenter presenter = new Presenter(HomeFragment.this);
presenter.onStart(Url.url1);
}
});
mTiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getContext(),Home2Activity.class));
}
});
}
@Override
protected int getFragmentLayoutId() {
return R.layout.homefragment;
}
@Override
public void onSuess(Object o) {
if (o instanceof Bean){
String author_name = ((Bean) o).getResult().getData().get(0).getAuthor_name();
Toast.makeText(getContext(), "成功"+author_name, Toast.LENGTH_SHORT).show();
Log.e("TAGH1",author_name);
mText1.setText(author_name);
}
}
@Override
public void onFalie(String string) {
Toast.makeText(getContext(), "失败1111111"+ string, Toast.LENGTH_SHORT).show();
Log.e("TAGH1ShiBai",string+"失败");
}
}
//————————————————————————-完———————————————————————————
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118361.html