package com.ly.supermvp.view.activity; import android.content.Context; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.inputmethod.InputMethodManager; import com.ly.supermvp.R; import com.ly.supermvp.adapter.SectionsPagerAdapter; import com.ly.supermvp.delegate.MainActivityDelegate; import com.ly.supermvp.mvp_frame.presenter.ActivityPresenter; import com.orhanobut.logger.Logger; import java.lang.reflect.Field; /** * <Pre> * 主Activity * </Pre> * @author 刘阳 * @version 1.0 * <p/> * Create by 2016/1/27 10:47 * @see https://github.com/liuyanggithub/SuperMvp */ public class MainActivity extends ActivityPresenter<MainActivityDelegate> implements View.OnClickListener { @Override protected Class<MainActivityDelegate> getDelegateClass() { return MainActivityDelegate.class; } /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link FragmentPagerAdapter} derivative, which will keep every * loaded fragment in memory. If this becomes too memory intensive, it * may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ private SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_main); // Create the adapter that will return a fragment for each of the three // primary sections of the activity. // Set up the ViewPager with the sections adapter. // mViewPager = (ViewPager) findViewById(R.id.container); // mViewPager.setAdapter(mSectionsPagerAdapter); // TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); // tabLayout.setupWithViewPager(mViewPager); // // FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); // fab.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) // .setAction("Action", null).show(); // } // }); } @Override protected void onDestroy() { super.onDestroy(); fixInputMethodManagerLeak(this); } @Override protected void initData() { super.initData(); mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), this); } @Override protected void initView() { super.initView(); setSwipeBackEnable(false); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); viewDelegate.setViewPagerAdapter(mSectionsPagerAdapter); viewDelegate.setupWithViewPager(); viewDelegate.setOnClickListener(this, R.id.fab); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.fab: Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); break; } } @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_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ /** * A placeholder fragment containing a simple view. */ /*public static class PlaceholderFragment extends Fragment { *//** * The fragment argument representing the section number for this * fragment. *//* private static final String ARG_SECTION_NUMBER = "section_number"; *//** * Returns a new instance of this fragment for the given section * number. *//* public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @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.section_label); ImageView imagView = (ImageView) rootView.findViewById(R.id.iv_desc); textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); return rootView; } }*/ /** * 解决InputMethodManager内存泄露现象 * @param destContext */ public static void fixInputMethodManagerLeak(Context destContext) { if (destContext == null) { return; } InputMethodManager imm = (InputMethodManager) destContext.getSystemService(Context.INPUT_METHOD_SERVICE); if (imm == null) { return; } String [] arr = new String[]{"mCurRootView", "mServedView", "mNextServedView"}; Field f = null; Object obj_get = null; for (int i = 0;i < arr.length;i ++) { String param = arr[i]; try{ f = imm.getClass().getDeclaredField(param); if (f.isAccessible() == false) { f.setAccessible(true); } // author: sodino mail:sodino@qq.com obj_get = f.get(imm); if (obj_get != null && obj_get instanceof View) { View v_get = (View) obj_get; if (v_get.getContext() == destContext) { // 被InputMethodManager持有引用的context是想要目标销毁的 f.set(imm, null); // 置空,破坏掉path to gc节点 } else { // 不是想要目标销毁的,即为又进了另一层界面了,不要处理,避免影响原逻辑,也就不用继续for循环了 Logger.d("fixInputMethodManagerLeak break, context is not suitable, get_context=" + v_get.getContext()+" dest_context=" + destContext); break; } } }catch(Throwable t){ t.printStackTrace(); } } } }