我有2个片段这是从一个活动的操作栏调用。两者都是GridView的,第一个显示用专用适配器的应用程序列表中,并且,第二个显示与另一个适配器的文件列表。我的问题是,当我启动的文件然后当我回到我的活动我切换从一个片段到另一个,我回来的时候到previous之一,它的内容消失。当我旋转的平板电脑我有一些问题,因为我的片段重新启动,以便为这个我认为删除片段给创建一个新的片段最新的可能性。
我如何管理更新的第一个片段的内容,而从第二个回来了吗?而如何才能重建新的片段行动旋转后删除片段?
:我活动的code是由以下code给出 公共类LaunchActivity延伸活动{ 公共静态活动发射; 私人PagerAdapter mPagerAdapter; 菜单菜单; 公共静态布尔更新=假的; 私有静态最后弦乐TAG =launchactivity; 公共静态字符串路径2; 公共静态的String [] mMenu; 公共静态字符串FILE_NAME =Seconde系列; 公共静态字符串teacher_file =老师; 公共静态字符串newString; 公共静态字符串SESSIONNAME; 公共静态的ArrayList<串GT; myList中; 私有静态的ArrayList<串GT;测试; 字符串用户=eleve; 串savedUser; 串模式=正常; 串savedMode; 私人诠释年; 私人诠释月; 私人诠释天; 静态最终诠释DATE_DIALOG_ID = 999; 公共静态布尔有效= FALSE; ////////////////////// OptionsActivity网络=新OptionsActivity(); 私人ApplicationManager AppManager的; ArrayList的<串GT;文件=新的ArrayList<串GT;(); 私人列表<串GT; LISTTITLE =新的ArrayList<串GT;(); 私人最终的BroadcastReceiver mApplicationsReceiver =新ApplicationsIntentReceiver(); 私人布尔mHomeDown; 私人布尔mBackDown; 私人动作条动作条; 文件mntFolder =新的文件(到/ mnt /); 文件[] =外部mntFolder.listFiles(); 公共静态布尔pause2 = FALSE; 公共静态字符串路径; 公共静态文件fpath; 私人布尔管理员= FALSE; 私人布尔eleve = FALSE; 私人布尔教授= FALSE; /////////////////////// 私有静态最后弦乐TAB_KEY_INDEX =tab_key; / **当第一次创建活动调用。 * / @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main1); LAUNCH =这一点; 如果(savedInstanceState == NULL){ 捆绑额外= getIntent()getExtras()。 如果(演员== NULL){ newString = NULL; }其他{ newString = extras.getString(preferenceConnector preF_NAME); } }其他{ newString =(字符串)savedInstanceState.getSerializable(preferenceConnector preF_NAME); } // Log.i(** ---- ----测试保存的值**,newString); 如果(newString!= NULL){ newString = newString.substring(1,(newString.length() - 1)); ArrayList的<串GT; SLIST =新的ArrayList<串GT;(Arrays.asList(newString.split())); myList中=新的ArrayList<串GT;(Arrays.asList(newString.split())); 串V =; myList.add(0,v.concat(sList.get(0))); myList.add(0,sList.get(0).concat(ⅴ)); ///////试试这个,以获得良好的尺寸/////////////////////////////// 的for(int i = 0; I< myList.size();我++){ Log.i(------> L'元素n+将String.valueOf(I),myList.get(I)); } } 如果(savedInstanceState!= NULL){ savedUser = savedInstanceState.getString(文本); savedMode = savedInstanceState.getString(MODE); 字符串str; 海峡= savedInstanceState.getString(ListActivity); 测试= savedInstanceState.getStringArrayList(目录); // myList中=(ArrayList的<串GT;)Arrays.asList(str.split(\ S *,\ S *)); }其他{ savedUser =eleve; savedMode =正常; } //Log.d("savedUser,savedUser); //Log.d("savedMode,savedMode); //动作条 动作条= getActionBar(); actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionbar.setDisplayUseLogoEnabled(假); actionbar.setDisplayShowTitleEnabled(假); //创建新的选项卡,并和设置选项卡的标题 ActionBar.Tab mFindTab = actionbar.newTab()的setText( 的getString(R.string.ui_tabname_find)); ActionBar.Tab mChatTab = actionbar.newTab()的setText( 的getString(R.string.ui_tabname_chat)); ActionBar.Tab mMeetTab = actionbar.newTab()的setText( 的getString(R.string.ui_tabname_meet)); ActionBar.Tab mPartyTab = actionbar.newTab()的setText( 的getString(R.string.ui_tabname_find)); //创建片段 片段mMeetFragment =新ApplicatinFragment(); 片段mFindFragment =新MatFragment(); 片段mChatFragment =新DocumentFragment1(); 片段mPartyFragment =新PartyFragment(); //绑定片段选项卡 - 设置tabListeners为每个标签 mFindTab.setTabListener(新MyTabsListener(mFindFragment, getApplicationContext())); mChatTab.setTabListener(新MyTabsListener(mChatFragment, getApplicationContext())); mMeetTab.setTabListener(新MyTabsListener(mMeetFragment, getApplicationContext()));// mPartyTab.setTabListener(新MyTabsListener(mPartyFragment,// getApplicationContext())); //标签添加到操作杆 actionbar.addTab(mMeetTab); actionbar.addTab(mFindTab); 如果(savedInstanceState!= NULL){ // Toast.makeText(getApplicationContext(), //选项卡+ savedInstanceState.getInt(TAB_KEY_INDEX,0), // Toast.LENGTH_SHORT).show(); actionbar.setSelectedNavigationItem(savedInstanceState.getInt( TAB_KEY_INDEX,0)); } } 公共无效onResume(){ this.stopService(新意图(这一点,fr.mi.DetectorService.class)); 意图startService =新意图(这一点,fr.mi.DetectorService.class); this.startService(startService); 如果(fr.mi.LockScreenActivity.LOCK!= NULL){ fr.mi.LockScreenActivity.LOCK.finish(); } Log.i(onResume,onResume()); 如果(AppList.ac!= NULL){ AppList.ac.finish(); } //Toast.makeText(thisonResume(),Toast.LENGTH_LONG).show(); super.onResume(); } @覆盖公共无效的onPause(){ super.onPause(); //始终首先调用父类的方法} 公共无效的onDestroy(){ super.onStop(); // tToast(的onDestroy。); } 私有类ApplicationsIntentReceiver扩展广播接收器{ @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ Toast.makeText(getApplicationContext(),测试+本,Toast.LENGTH_LONG).show(); appManager.loadApplications(假); } } @覆盖 保护无效的onSaveInstanceState(捆绑outState){ super.onSaveInstanceState(outState); / * Toast.makeText( 这个, 的onSaveInstanceState:选项卡 + getActionBar()。getSelectedNavigationIndex(), Toast.LENGTH_SHORT).show(); * / outState.putStringArrayList(列表,LaunchActivity.myList); outState.putString(ListActivity,LaunchActivity.myList.toString()); } 私人无效registerIntentReceivers(){ IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addDataScheme(包); registerReceiver(mApplicationsReceiver,过滤器); }}类MyTabsListener实现ActionBar.TabListener { 公共片段片段; 公共上下文的背景下; 公共MyTabsListener(片段片段,上下文的背景下){ this.fragment =片段; this.context =背景; } @覆盖 公共无效onTabReselected(标签选项卡,FragmentTransaction英尺){ // Toast.makeText(上下文中,复选!,Toast.LENGTH_SHORT).show(); } @覆盖 公共无效onTabSelected(标签选项卡,FragmentTransaction英尺){ //Toast.makeText(context评选!,Toast.LENGTH_SHORT).show(); ft.replace(R.id.fragment_container,片段); } @覆盖 公共无效onTabUnselected(标签选项卡,FragmentTransaction英尺){ //Toast.makeText(context!未选定Toast.LENGTH_SHORT).show(); ft.remove(片段); }}
我使用一个GridView查看安装的应用程序第一个片段。由以下code给出:
公共类ApplicatinFragment扩展片段{ TextView的称号; 私有静态ApplicationManager AppManager的; 菜单菜单; 公共静态布尔更新=假的; 私有静态最后弦乐TAG =launchactivity; 公共静态字符串路径; 公共静态的String [] mMenu; 公共静态字符串FILE_NAME =Seconde系列; 公共静态字符串teacher_file =老师; //私有静态最后弦乐preF_APPLICATION_LIST =application_list; // OptionsActivity网络=新OptionsActivity(); 私有静态最后弦乐preF_SERVICE_ENABLED =service_enabled; 私有静态最后弦乐preF_APPLICATION_LIST =application_list; 私有静态最后弦乐preF_APPLICATION_LIST1 =application_list1; 私有静态最后弦乐preF_APPLICATION_LISTPROF =application_list_prof; 私有静态最后弦乐preF_AUTO_START =start_service_after_boot; 私有静态最后弦乐preF_PASSWORD =密码; 私有静态最后弦乐preF_UNIVERSEL_PASSWORD =universelpassword; 公共静态布尔SBACKSTUCK = FALSE; 私人共享preferences米preF; 私人布尔mServiceEnabled,mAutoStart; 公共静态的String [] mApplicationList; 私有String [] mApplicationList1; 私有String [] mApplicationListProf; 公共静态字符串mPassword; 私人字符串mUniverselPassword; 私人诠释mRelockTimeout; 私人最终的BroadcastReceiver mApplicationsReceiver =新ApplicationsIntentReceiver(); GridView控件GridView控件; ArrayList的&LT;串GT;文件=新的ArrayList&LT;串GT;(); 私有静态GridViewAppInfoAdapter适配器; @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); Log.i(PageAdminFragment,的onCreate); AppManager的=新ApplicationManager(getActivity(),getActivity() .getPackageManager(),LaunchActivity.myList); appManager.loadApplications(真); registerIntentReceivers(); 适配器=新GridViewAppInfoAdapter(getActivity() appManager.getStudentApplications code()); 共享preferences共享preferences = preferenceManager.getDefaultShared preferences(getActivity()); 共享preferences.Editor编辑=共享preferences.edit();// mApplicationList = M pref.getString(preF_APPLICATION_LIST,).split(); 串组合=; 合并的=合并的+com.android.vending+,; 合并的=合并的+com.android.settings+,; 合并的=合并的+com.android.packageinstaller+, ; //合并= +综合air.Arbres 的for(int i = 0; I&LT; SessionChoose.ProtectedApplication.size();我++){ 合并的=合并的+ SessionChoose.ProtectedApplication.toString()+; ; } Log.d( - * - * LES应用proteger - * - *,合并); //Toast.makeText(getActivity(),"-*-*les应用proteger - * - *+结合,Toast.LENGTH_LONG).show(); // editor.putString(preF_APPLICATION_LIST,合并).commit(); //加载preferences(getActivity()); //Toast.makeText(getApplicationContext(),"$p$pF_APPLICATION_LIST+ strSavedMem2,Toast.LENGTH_LONG).show(); // SaveApplication preferences(preF_APPLICATION_LIST,strSavedMem2,ProtectedApplication,getApplicationContext()); //加载preferences(getActivity()); //Toast.makeText(getApplicationContext(),"$p$pF_APPLICATION_LIST+ strSavedMem2,Toast.LENGTH_LONG).show(); //加载preferences(getActivity()); / ** *科瑞文件来保存会话内容 * / 。this.getActivity()getApplicationContext()stopService(新意图(this.getActivity(),fr.mi.DetectorService.class)); //getActivity().stopService(new意图(this.getActivity(),fr.mi.DetectorService.class)); } @覆盖 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup, 捆绑savedInstanceState){ //Toast.makeText(getActivity(),我需要好适配器,Toast.LENGTH_SHORT).show(); Log.d(TAG,appManager.getStudentApplications()的toString()); INT display_mode = getResources()getConfiguration()方向。; 查看查看= NULL; ////////////////////////////////////////////////// /////////////////////////// 如果(display_mode == 1){ 鉴于= inflater.inflate(R.layout.main_grid, 集装箱,FALSE); // this.getWindow()。setFlags( // WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); // this.getWindow()clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); GridView控件=(GridView控件)view.findViewById(R.id.gridview); // MGRID =(GridView控件)findViewById(R.id.gridview); gridView.setColumnWidth(95); // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); }其他{ 鉴于= inflater.inflate(R.layout.main_grid_land, 集装箱,FALSE); // this.getWindow()。setFlags( // WindowManager.LayoutParams.FLAG_FULLSCREEN, // WindowManager.LayoutParams.FLAG_FULLSCREEN); //setContentView(R.layout.main_grid_land); GridView控件=(GridView控件)view.findViewById(R.id.gridview1); gridView.setColumnWidth(95); Log.d(模式,土地); // mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); } gridView.setAdapter(适配器); gridView.setSelection(0); /////////////尝试这个 /////////////////////// 共享preferences共享preferences = preferenceManager.getDefaultShared preferences(this.getActivity()getApplicationContext()); 共享preferences.Editor编辑=共享preferences.edit(); gridView.setOnItemClickListener((新OnItemClickListener(){ 上派河畔L'项目// SI @覆盖 公共无效onItemClick(适配器视图&LT;&GT;母公司,视图V, INT位置,长的id){ ApplicationInfo应用=(ApplicationInfo)父 .getItemAtPosition(位置); startActivity(app.intent); } })); 如果(ViewPagerActivity.VIEWDOC ==真){ 意向意图=新的Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.setClass(getActivity(),LaunchActivity.class); startActivity(意向); } 意图startService =新意图(this.getActivity()getApplicationContext(),fr.mi.DetectorService.class。); 。getActivity()startService(startService); ViewPagerActivity.VIEWDOC = FALSE; 返回视图。 } 私人无效bindApplications(LayoutInflater充气器,容器的ViewGroup){ 查看查看= inflater.inflate(R.layout.main_grid, 集装箱,FALSE); 如果(GridView控件== NULL){ GridView控件=(GridView控件)view.findViewById(R.id.gridview); } 适配器=新GridViewAppInfoAdapter(getActivity() appManager.getStudentApplications code()); adapter.notifyDataSetChanged(); gridView.setAdapter(适配器); gridView.setSelection(0); } 私人无效registerIntentReceivers(){ IntentFilter的过滤器=新的IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addDataScheme(包); 。getActivity()registerReceiver(mApplicationsReceiver,过滤器); } @覆盖 公共无效的onStop(){ super.onStop(); Log.i(TAG的onStop); } @覆盖 公共无效的onDestroy(){ Log.i(TAGOnsdESTROY); super.onDestroy(); } @覆盖 公共无效onResume(){ Log.i(TAGOnResume); super.onResume(); } 私人无效重装preferences(){ mServiceEnabled = M pref.getBoolean(preF_SERVICE_ENABLED,FALSE); mApplicationList = M pref.getString(preF_APPLICATION_LIST,).split(); mApplicationList1 = M pref.getString(preF_APPLICATION_LIST1,).split(); mApplicationListProf = M pref.getString(preF_APPLICATION_LISTPROF,)分裂。(); //Log.d("**************La清单当然DES应用proteger ***********,mApplicationList.toString()); Log.i(+ - + - + - + - + - ***截尾*** ++++++++++,mApplicationList.toString()); mAutoStart = M pref.getBoolean(preF_AUTO_START,FALSE); mPassword = M pref.getString(preF_PASSWORD,2492); mUniverselPassword = M pref.getString(preF_UNIVERSEL_PASSWORD,9113); 如果(M pref.getBoolean(relock_policy,真)){ 尝试{ mRelockTimeout =的Integer.parseInt(M pref.getString(relock_timeout, - 1)); }赶上(例外五){ mRelockTimeout = -1; } }其他{ mRelockTimeout = -1; } } / ** *接收添加应用程序时/删除通知。 * / 私有类ApplicationsIntentReceiver扩展广播接收器{ LayoutInflater吹气; 集装箱的ViewGroup; 公共ApplicationsIntentReceiver(){ } / *公共ApplicationsIntentReceiver(LayoutInflater充气器,容器的ViewGroup){ this.inflater =吹气; this.container =容器; } * / @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ // =的AppManager新ApplicationManager(getActivity(),getActivity() // getPackageManager(),NULL); SBACKSTUCK =真; appManager.loadApplications(假); // gridView.setAdapter(NULL); // GridViewAppInfoAdapter适配器1 =新GridViewAppInfoAdapter(getActivity() // appManager.getStudentApplications code()); //adapter1.setApps((List<ApplicationInfo>)appManager.getStudentApplications code()); //gridView.setAdapter(adapter1); // Toast.makeText(getActivity(),拉valeur+ LaunchActivity.myList.toString(),Toast.LENGTH_LONG).show(); Log.d(香格里拉valeur德拉名单,LaunchActivity.myList.toString()); //适配器=新GridViewAppInfoAdapter(getActivity() // appManager.getStudentApplications code()); bindApplications(吹气,集装箱); } } ////实现绑定应用程序 私人无效bindApplications(适配器视图&LT;&GT;母公司,INT位置){ ApplicationInfo应用=(ApplicationInfo)父 .getItemAtPosition(位置); startActivity(app.intent); } // /////////////////////////////////////////////// / ** *启动网格视图中选定活动/应用。 * / 私有类ApplicationLauncher工具 AdapterView.OnItemClickListener { @覆盖 公共无效onItemClick(适配器视图父母,视图V,INT位置, 长ID){ ApplicationInfo应用=(ApplicationInfo)父 .getItemAtPosition(位置); startActivity(app.intent); } } // ////////////////////////////////////////////// 公共布尔的onkeydown(INT K,KeyEvent的EV){ 返回false; } 公共无效onBack pressed(){ 意向意图=新的Intent(); intent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(意向); //完成(); }}
第二个片段是用来查看文件的GridView控件是由下面的code给出:
公共类MatFragment扩展片段{ 私人GridViewAjoutFile适配器; 私人GridViewAjoutFile ajout =新GridViewAjoutFile(); 公共静态布尔continuer = FALSE; 公共静态GridviewAdapter mAdapter; 公共静态的ArrayList&LT;串GT; listCountry =新的ArrayList&LT;串GT;(); 公共静态的ArrayList&LT;整数GT; listFlag =新的ArrayList&LT;整数GT;(); 私人的ArrayList&LT;串GT; intentType; 私人GridView控件GridView控件; 私人字符串mStackLevel =; @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); } @覆盖 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup, 捆绑savedInstanceState){ 最终的ArrayList&LT;串GT; profilTabShortCut; 查看查看= inflater.inflate(R.layout.maindoc, 集装箱,FALSE); 如果(savedInstanceState!= NULL){ mStackLevel = savedInstanceState.getString(级别); } prepareList(); mAdapter =新GridviewAdapter(getActivity(),listCountry,listFlag); //mAdapter.setFile(listCountry,listFlag); GridView控件=(GridView控件)view.findViewById(R.id.gridView2); //gridView.setAdapter(null); gridView.setAdapter(mAdapter); //落实项目点击监听器 gridView.setOnItemClickListener(新AdapterView.OnItemClickListener() { @覆盖 公共无效onItemClick(适配器视图&LT;&GT;为arg0,ARG1查看,INT位置, 长ARG3){SetIntentType(SessionChoose.listofUrl.get(位置),intentType.get(位置)); } }); 返回视图。 } @覆盖 公共无效的onSaveInstanceState(捆绑outState){ super.onSaveInstanceState(outState); outState.putString(级别,listCountry.toString()); } @覆盖 公共无效onResume(){ Log.e(调试,LoginFragment的onResume); listCountry = removeDuplicate(listCountry); // listFlag = removeDuplicate1(listFlag); super.onResume(); } 公共静态的ArrayList&LT;串GT; removeDuplicate(ArrayList的&LT;串GT; arlList){ HashSet的H =新的HashSet(arlList); arlList.clear(); arlList.addAll(H); 返回arlList; } 公共静态的ArrayList&LT;整数GT; removeDuplicate1(ArrayList的&LT;整数GT; arlList){ HashSet的H =新的HashSet(arlList); arlList.clear(); arlList.addAll(H); 返回arlList; } 公共意向SetIntentType(URL字符串,字符串intenttype){ 意向意图=新的Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); 返回null; } 公共无效prepareList() {intentType =新的ArrayList&LT;串GT;(); listCountry.clear(); listFlag.clear(); 如果(SessionChoose.listofDoc.size()大于0){ 的for(int i = 0; I&LT; SessionChoose.listofDoc.size();我++){ listCountry.add(SessionChoose.listofDoc.get(I)); 串filenameArray [] = SessionChoose.listofDoc.get(ⅰ).split(\。); 字符串延长= filenameArray [filenameArray.length-1]; } @覆盖 公共无效setUserVisibleHint(布尔isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); 如果(isVisibleToUser){ } } 私有类ApplicationsIntentReceiver扩展广播接收器{ @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ // appManager.loadApplications(假); //意图INTE =新意图(getActivity(),LauncherGridActivity.class); // startActivity(INTE); } }}
解决方案
您可以使用 -
@覆盖 公共无效setUserVisibleHint(布尔isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); 如果(isVisibleToUser){ } }
I have 2 fragments which are called from the action bar of an activity. Both are gridviews, the first one displays an application list with a dedicated adapter, and the second one displays a file list with another adapter. My problem is that when I launch a file then when I back to my activity I switch from one fragment to another, when I come back to the previous one, its content disappears. And when I rotate tablet I have the some problem, because my Fragment restart so for this I think that removing fragment give the possibility to create a new Fragment up to date.
How can I manage to update the content of the first fragment while coming back from the second one ? And how to remove fragment after the rotation in order to recreate the Action with new Fragment? the code of my Activity is given by the code below:
public class LaunchActivity extends Activity {
public static Activity LAUNCH;
private PagerAdapter mPagerAdapter;
Menu menu;
public static boolean updated = false;
private static final String TAG = "launchactivity";
public static String Path2;
public static String[] mMenu;
public static String file_name = "seconde";
public static String teacher_file = "teacher";
public static String newString;
public static String SESSIONNAME;
public static ArrayList<String> myList;
private static ArrayList<String> test;
String user = "eleve";
String savedUser;
String mode = "normal";
String savedMode;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
public static boolean active = false;
////////////////////
// OptionsActivity fi = new OptionsActivity();
private ApplicationManager appManager;
ArrayList<String> files = new ArrayList<String>();
private List<String> listTitle = new ArrayList<String>();
private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
private boolean mHomeDown;
private boolean mBackDown;
private ActionBar actionbar ;
File mntFolder = new File("/mnt/");
File[] external = mntFolder.listFiles();
public static boolean pause2=false;
public static String path;
public static File fpath;
private boolean admin = false;
private boolean eleve = false;
private boolean prof = false;
///////////////////////
private static final String TAB_KEY_INDEX = "tab_key";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
LAUNCH = this;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString(PreferenceConnector.PREF_NAME);
}
} else {
newString= (String) savedInstanceState.getSerializable(PreferenceConnector.PREF_NAME);
}
// Log.i("**----test----saved value**" , newString);
if(newString != null){
newString = newString.substring(1 , (newString.length()-1));
ArrayList<String> sList = new ArrayList<String>(Arrays.asList(newString.split(",")));
myList = new ArrayList<String>(Arrays.asList(newString.split(", ")));
String v = " ";
myList.add(0, v.concat(sList.get(0)));
myList.add(0, sList.get(0).concat(v));
/////// try this in order to get the good size///////////////////////////////
for(int i= 0 ; i< myList.size() ; i++){
Log.i("------> l'element n " + String.valueOf(i),myList.get(i));
}
}
if (savedInstanceState != null) {
savedUser = savedInstanceState.getString("TEXT");
savedMode = savedInstanceState.getString("MODE");
String str;
str = savedInstanceState.getString("ListActivity");
test = savedInstanceState.getStringArrayList("List");
// myList = (ArrayList<String>) Arrays.asList(str.split("s*,s*"));
} else {
savedUser = "eleve";
savedMode = "normal";
}
//Log.d("savedUser", savedUser);
//Log.d("savedMode", savedMode);
// ActionBar
actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayUseLogoEnabled(false);
actionbar.setDisplayShowTitleEnabled(false) ;
// create new tabs and and set up the titles of the tabs
ActionBar.Tab mFindTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_find));
ActionBar.Tab mChatTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_chat));
ActionBar.Tab mMeetTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_meet));
ActionBar.Tab mPartyTab = actionbar.newTab().setText(
getString(R.string.ui_tabname_find));
// create the fragments
Fragment mMeetFragment = new ApplicatinFragment();
Fragment mFindFragment = new MatFragment();
Fragment mChatFragment = new DocumentFragment1();
Fragment mPartyFragment = new PartyFragment();
// bind the fragments to the tabs - set up tabListeners for each tab
mFindTab.setTabListener(new MyTabsListener(mFindFragment,
getApplicationContext()));
mChatTab.setTabListener(new MyTabsListener(mChatFragment,
getApplicationContext()));
mMeetTab.setTabListener(new MyTabsListener(mMeetFragment,
getApplicationContext()));
// mPartyTab.setTabListener(new MyTabsListener(mPartyFragment,
// getApplicationContext()));
// add the tabs to the action bar
actionbar.addTab(mMeetTab);
actionbar.addTab(mFindTab);
if (savedInstanceState != null) {
// Toast.makeText(getApplicationContext(),
// "tab is " + savedInstanceState.getInt(TAB_KEY_INDEX, 0),
// Toast.LENGTH_SHORT).show();
actionbar.setSelectedNavigationItem(savedInstanceState.getInt(
TAB_KEY_INDEX, 0));
}
}
public void onResume() {
this.stopService(new Intent(this, fr.mi.DetectorService.class));
Intent startService = new Intent(this, fr.mi.DetectorService.class);
this.startService(startService);
if(fr.mi.LockScreenActivity.LOCK != null){
fr.mi.LockScreenActivity.LOCK.finish();
}
Log.i("onResume", "onResume()");
if (AppList.ac != null) {
AppList.ac.finish();
}
//Toast.makeText(this, "onResume()", Toast.LENGTH_LONG).show();
super.onResume();
}
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
}
public void onDestroy() {
super.onStop();
// tToast("onDestroy.");
}
private class ApplicationsIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(getApplicationContext(), "test" + "this" , Toast.LENGTH_LONG).show();
appManager.loadApplications(false);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/*
Toast.makeText(
this,
"onSaveInstanceState: tab is"
+ getActionBar().getSelectedNavigationIndex(),
Toast.LENGTH_SHORT).show(); */
outState.putStringArrayList("List",LaunchActivity.myList);
outState.putString("ListActivity", LaunchActivity.myList.toString());
}
private void registerIntentReceivers() {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
registerReceiver(mApplicationsReceiver, filter);
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public Context context;
public MyTabsListener(Fragment fragment, Context context) {
this.fragment = fragment;
this.context = context;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// Toast.makeText(context, "Reselected!", Toast.LENGTH_SHORT).show();
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(context, "Selected!", Toast.LENGTH_SHORT).show();
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(context, "Unselected!", Toast.LENGTH_SHORT).show();
ft.remove(fragment);
}
}
My first Fragment used to view installed application in a gridView. is given by the code below:
public class ApplicatinFragment extends Fragment {
TextView title;
private static ApplicationManager appManager;
Menu menu;
public static boolean updated = false;
private static final String TAG = "launchactivity";
public static String Path;
public static String[] mMenu;
public static String file_name = "seconde";
public static String teacher_file = "teacher";
//private static final String PREF_APPLICATION_LIST = "application_list";
//OptionsActivity fi = new OptionsActivity();
private static final String PREF_SERVICE_ENABLED = "service_enabled";
private static final String PREF_APPLICATION_LIST = "application_list";
private static final String PREF_APPLICATION_LIST1 = "application_list1";
private static final String PREF_APPLICATION_LISTPROF = "application_list_prof";
private static final String PREF_AUTO_START = "start_service_after_boot";
private static final String PREF_PASSWORD = "password";
private static final String PREF_UNIVERSEL_PASSWORD = "universelpassword";
public static boolean SBACKSTUCK = false ;
private SharedPreferences mPref;
private boolean mServiceEnabled, mAutoStart;
public static String[] mApplicationList;
private String[] mApplicationList1;
private String[] mApplicationListProf;
public static String mPassword;
private String mUniverselPassword;
private int mRelockTimeout;
private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver();
GridView gridView;
ArrayList<String> files = new ArrayList<String>();
private static GridViewAppInfoAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("PageAdminFragment", "onCreate");
appManager = new ApplicationManager(getActivity(), getActivity()
.getPackageManager(),LaunchActivity.myList);
appManager.loadApplications(true);
registerIntentReceivers();
adapter = new GridViewAppInfoAdapter(getActivity(),
appManager.getStudentApplicationsCode());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
// mApplicationList = mPref.getString(PREF_APPLICATION_LIST, "").split(";");
String combined = "";
combined = combined + "com.android.vending" + ";";
combined = combined + "com.android.settings" + ";";
combined = combined + "com.android.packageinstaller" + ";" ;
//combined = combined + "air.Arbres";
for( int i = 0; i < SessionChoose.ProtectedApplication.size(); i++){
combined = combined + SessionChoose.ProtectedApplication.toString() + ";" ;
}
Log.d("-*-*les applications à proteger-*-*", combined);
//Toast.makeText(getActivity(),"-*-*les applications à proteger-*-*"+ combined,Toast.LENGTH_LONG).show();
// editor.putString(PREF_APPLICATION_LIST, combined).commit();
// LoadPreferences(getActivity());
//Toast.makeText(getApplicationContext(),"PREF_APPLICATION_LIST " + strSavedMem2 , Toast.LENGTH_LONG ).show();
// SaveApplicationPreferences(PREF_APPLICATION_LIST,strSavedMem2 ,ProtectedApplication,getApplicationContext());
//LoadPreferences(getActivity());
//Toast.makeText(getApplicationContext(),"PREF_APPLICATION_LIST " + strSavedMem2 , Toast.LENGTH_LONG ).show();
//LoadPreferences(getActivity());
/**
* Creat file to save session Content
*/
this.getActivity().getApplicationContext().stopService(new Intent(this.getActivity(), fr.mi.DetectorService.class));
//getActivity().stopService(new Intent(this.getActivity(), fr.mi.DetectorService.class));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Toast.makeText(getActivity(), "I need the good Adapter", Toast.LENGTH_SHORT).show();
Log.d(TAG, appManager.getStudentApplications().toString());
int display_mode = getResources().getConfiguration().orientation;
View view = null;
/////////////////////////////////////////////////////////////////////////////
if (display_mode == 1) {
view = inflater.inflate(R.layout.main_grid,
container, false);
// this.getWindow().setFlags(
// WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
// this.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
gridView = (GridView) view.findViewById(R.id.gridview);
//mGrid = (GridView) findViewById(R.id.gridview);
gridView.setColumnWidth(95);
// mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
} else {
view = inflater.inflate(R.layout.main_grid_land,
container, false);
// this.getWindow().setFlags(
// WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
//setContentView(R.layout.main_grid_land);
gridView = (GridView) view.findViewById(R.id.gridview1);
gridView.setColumnWidth(95);
Log.d("Mode", "land");
// mGrid.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
gridView.setAdapter(adapter);
gridView.setSelection(0);
/////////////try this ///////////////////////
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this.getActivity().getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
gridView.setOnItemClickListener((new OnItemClickListener() {
// si on clique sur l'item
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ApplicationInfo app = (ApplicationInfo) parent
.getItemAtPosition(position);
startActivity(app.intent);
}
}));
if(ViewPagerActivity.VIEWDOC == true ){
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setClass(getActivity(), LaunchActivity.class);
startActivity(intent);
}
Intent startService = new Intent(this.getActivity().getApplicationContext(), fr.mi.DetectorService.class);
getActivity().startService(startService);
ViewPagerActivity.VIEWDOC = false ;
return view;
}
private void bindApplications(LayoutInflater inflater, ViewGroup container) {
View view = inflater.inflate(R.layout.main_grid,
container, false);
if (gridView == null) {
gridView = (GridView) view.findViewById(R.id.gridview);
}
adapter = new GridViewAppInfoAdapter(getActivity(),
appManager.getStudentApplicationsCode());
adapter.notifyDataSetChanged();
gridView.setAdapter(adapter);
gridView.setSelection(0);
}
private void registerIntentReceivers() {
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
getActivity().registerReceiver(mApplicationsReceiver, filter);
}
@Override
public void onStop() {
super.onStop();
Log.i(TAG, "Onstop");
}
@Override
public void onDestroy() {
Log.i(TAG, "OnsdESTROY");
super.onDestroy();
}
@Override
public void onResume() {
Log.i(TAG, "OnResume");
super.onResume();
}
private void reloadPreferences() {
mServiceEnabled = mPref.getBoolean(PREF_SERVICE_ENABLED, false);
mApplicationList = mPref.getString(PREF_APPLICATION_LIST, "").split(";");
mApplicationList1 = mPref.getString(PREF_APPLICATION_LIST1, "").split(";");
mApplicationListProf = mPref.getString(PREF_APPLICATION_LISTPROF,"").split(";");
//Log.d("**************La Liste des applications à proteger***********", mApplicationList.toString());
Log.i("+-+-+-+-+- ***Censored***++++++++++", mApplicationList.toString() );
mAutoStart = mPref.getBoolean(PREF_AUTO_START, false);
mPassword = mPref.getString(PREF_PASSWORD, "2492");
mUniverselPassword = mPref.getString(PREF_UNIVERSEL_PASSWORD , "9113");
if (mPref.getBoolean("relock_policy", true)){
try{
mRelockTimeout = Integer.parseInt(mPref.getString("relock_timeout", "-1"));
}catch(Exception e){
mRelockTimeout = -1;
}
}else{
mRelockTimeout = -1;
}
}
/**
* Receives notifications when applications are added/removed.
*/
private class ApplicationsIntentReceiver extends BroadcastReceiver {
LayoutInflater inflater;
ViewGroup container ;
public ApplicationsIntentReceiver(){
}
/* public ApplicationsIntentReceiver(LayoutInflater inflater, ViewGroup container){
this.inflater = inflater ;
this.container = container ;
}*/
@Override
public void onReceive(Context context, Intent intent) {
// appManager = new ApplicationManager(getActivity(), getActivity()
//.getPackageManager(), null);
SBACKSTUCK = true;
appManager.loadApplications(false);
// gridView.setAdapter(null);
//GridViewAppInfoAdapter adapter1 = new GridViewAppInfoAdapter(getActivity(),
// appManager.getStudentApplicationsCode());
//adapter1.setApps((List<ApplicationInfo>) appManager.getStudentApplicationsCode());
//gridView.setAdapter(adapter1);
// Toast.makeText(getActivity(), "la valeur" + LaunchActivity.myList.toString() , Toast.LENGTH_LONG).show();
Log.d("la valeur de la list" ,LaunchActivity.myList.toString() );
//adapter = new GridViewAppInfoAdapter(getActivity(),
// appManager.getStudentApplicationsCode());
bindApplications( inflater , container);
}
}
////implement the bind Application
private void bindApplications(AdapterView<?> parent, int position){
ApplicationInfo app = (ApplicationInfo) parent
.getItemAtPosition(position);
startActivity(app.intent);
}
// ///////////////////////////////////////////////
/**
* Starts the selected activity/application in the grid view.
*/
private class ApplicationLauncher implements
AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
ApplicationInfo app = (ApplicationInfo) parent
.getItemAtPosition(position);
startActivity(app.intent);
}
}
// //////////////////////////////////////////////
public boolean onKeyDown(int k, KeyEvent ev) {
return false;
}
public void onBackPressed() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// finish();
}
}
The second fragment is used to view a gridview of file is given by the code below:
public class MatFragment extends Fragment {
private GridViewAjoutFile adapter;
private GridViewAjoutFile ajout = new GridViewAjoutFile();
public static boolean continuer = false;
public static GridviewAdapter mAdapter ;
public static ArrayList<String> listCountry = new ArrayList<String>();
public static ArrayList<Integer> listFlag = new ArrayList<Integer>();
private ArrayList<String> intentType;
private GridView gridView;
private String mStackLevel = " ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ArrayList<String> profilTabShortCut;
View view = inflater.inflate(R.layout.maindoc,
container, false);
if (savedInstanceState != null) {
mStackLevel = savedInstanceState.getString("level");
}
prepareList();
mAdapter = new GridviewAdapter(getActivity(),listCountry, listFlag);
//mAdapter.setFile(listCountry , listFlag);
gridView = (GridView) view.findViewById(R.id.gridView2);
//gridView.setAdapter(null);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) { SetIntentType(SessionChoose.listofUrl.get(position),intentType.get(position));
}
});
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("level", listCountry.toString());
}
@Override
public void onResume() {
Log.e("DEBUG", "onResume of LoginFragment");
listCountry = removeDuplicate(listCountry);
//listFlag = removeDuplicate1(listFlag);
super.onResume();
}
public static ArrayList<String> removeDuplicate(ArrayList<String> arlList) {
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
return arlList;
}
public static ArrayList<Integer> removeDuplicate1(ArrayList<Integer> arlList) {
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
return arlList;
}
public Intent SetIntentType(String url, String intenttype ){
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
return null;
}
public void prepareList()
{ intentType = new ArrayList<String>();
listCountry.clear();
listFlag.clear();
if(SessionChoose.listofDoc.size()>0){
for(int i = 0; i< SessionChoose.listofDoc.size() ; i++){
listCountry.add(SessionChoose.listofDoc.get(i));
String filenameArray[] = SessionChoose.listofDoc.get(i).split(".");
String extension = filenameArray[filenameArray.length-1];
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
}
private class ApplicationsIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// appManager.loadApplications(false);
// Intent inte = new Intent(getActivity(),LauncherGridActivity.class);
//startActivity(inte);
}
}
}
解决方案
You can use -
@Override
public void setUserVisibleHint(boolean isVisibleToUser)
{
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
}
}
相关推荐
最新文章