我的应用程序表现得非常奇怪,我在我的活动布局中使用FrameLayout来膨胀片段类,如下所示:
if (mapFragment == null) {
mapFragment = MapFragment.newInstance();
addFragment(mapFragment, null, MapFragment.class.getCanonicalName(), false, MainActivity.this);
} else {
addFragment(mapFragment, null, MapFragment.class.getCanonicalName(), false, MainActivity.this);
}
奇怪的是,用户第一次通过单击按钮来运行此代码时,我的整个活动重新启动,逻辑上不再显示该片段。这是addFragment方法:
void addFragment(Fragment frag, Bundle bundle, String backstack,
boolean forceReplace, FragmentActivity _activity) {
Log.e("Addfragment Method", "Fragment Added");
FragmentManager fm = _activity.getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(getRes());
FragmentTransaction ft = fm.beginTransaction();
if (bundle != null) {
frag.setArguments(bundle);
}
if (fragment != null || forceReplace) {
ft.replace(getRes(), frag, backstack);
ft.addToBackStack(backstack);
Log.e("replaced","fragment");
} else {
ft.add(getRes(), frag, backstack);
ft.addToBackStack(backstack);
Log.e("created","fragment");
}
ft.commitAllowingStateLoss();
}
这里是片段onCreateView方法
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Utils.sendTrackerDetails(this.getClass().getSimpleName());
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.fragment_map, container, false);
} catch (InflateException e) {
}
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
}
view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
gpsTracker = new GPSTracker(getActivity());
mLayoutMap = (RelativeLayout) view.findViewById(R.id.layout_map);
mEmptyText = (CustomFontTextview) view.findViewById(R.id.empty_text);
mTextSearch = (EditText) view.findViewById(R.id.search_text);
mPocketBar = (SmoothProgressBar) view.findViewById(R.id.pocket);
mPocketBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(getActivity()).interpolator(new AccelerateInterpolator()).build());
text_header = (TextView) view.findViewById(R.id.text_header);
search_text = (EditText) view.findViewById(R.id.search_text);
liste = (ListView) view.findViewById(R.id.list_dab);
mTextSearch.setEnabled(false);
if (Utils.checkGooglePlayServicesAvailability(getActivity()) || Utils.isConnectedTointernet(getActivity())) {
//MapsInitializer.initialize(getActivity());
if (mGoogleMap == null)
initilizeMap();
else {
if (Status.equals(Constants.TAG_CLASS_DAB)) {
icon = BitmapDescriptorFactory
.fromResource(R.drawable.epingle_dab);
mListDabAsynck = new LoadDABAsyncTask();
mListDabAsynck.execute();
} else {
icon = BitmapDescriptorFactory
.fromResource(R.drawable.epingle);
mListAgenceAsynck = new LoadAgencesAsyncTask();
mListAgenceAsynck.execute();
}
}
} else {
Utils.showErrorCrouton(getActivity(), getResources().getString(R.string.text_service_nondisponible));
}
if (Status.equals(Constants.TAG_CLASS_DAB)) {
text_header.setText(getActivity().getResources().getString(R.string.trouver_agence));
search_text.setHint(getActivity().getResources().getString(R.string.trouver_agence));
} else {
text_header.setText(getActivity().getResources().getString(R.string.trouver_agence_banque));
search_text.setHint(getActivity().getResources().getString(R.string.trouver_agence_banque));
}
btn_liste = (ImageView) view.findViewById(R.id.btn_liste);
btn_liste.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Status.equals(Constants.TAG_CLASS_AGENCE)) {
mTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = mTextSearch.getText().toString()
.toLowerCase(Locale.getDefault());
if (!(listAgenceAdapter == null)) {
listAgenceAdapter.filter(text);
if (listAgenceAdapter.isEmpty()) {
mEmptyText.setVisibility(View.VISIBLE);
liste.setVisibility(View.GONE);
} else {
mEmptyText.setVisibility(View.GONE);
liste.setVisibility(View.VISIBLE);
}
}
}
});
if (tagName.equals("liste")) {
btn_liste.setImageDrawable(getResources().getDrawable(R.drawable.btn_liste));
text_header.setText(getActivity().getResources().getString(R.string.trouver_agence_banque));
flip(mLayoutMap, liste, DURATION);
liste.setVisibility(View.GONE);
tagName = "map";
mTextSearch.setEnabled(true);
mTextSearch.setText("");
mEmptyText.setVisibility(View.GONE);
} else {
btn_liste.setImageDrawable(getResources().getDrawable(R.drawable.btn_liste));
flip(liste, mLayoutMap, DURATION);
tagName = "liste";
mTextSearch.setEnabled(false);
mEmptyText.setVisibility(View.GONE);
mTextSearch.setText("");
mEmptyText.setVisibility(View.GONE);
}
} else {
mTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = mTextSearch.getText().toString()
.toLowerCase(Locale.getDefault());
if (!(listDabAdapter == null)) {
listDabAdapter.filter(text);
if (listDabAdapter.isEmpty()) {
mEmptyText.setVisibility(View.VISIBLE);
liste.setVisibility(View.GONE);
} else {
mEmptyText.setVisibility(View.GONE);
liste.setVisibility(View.VISIBLE);
}
}
}
});
if (tagName.equals("liste")) {
btn_liste.setImageDrawable(getResources().getDrawable(R.drawable.btn_map));
text_header.setText(getActivity().getResources().getString(R.string.trouver_agence));
flip(mLayoutMap, liste, DURATION);
liste.setVisibility(View.GONE);
tagName = "map";
mTextSearch.setEnabled(true);
mTextSearch.setText("");
mEmptyText.setVisibility(View.GONE);
} else {
btn_liste.setImageDrawable(getResources().getDrawable(R.drawable.btn_liste));
flip(liste, mLayoutMap, DURATION);
tagName = "liste";
mTextSearch.setEnabled(false);
mEmptyText.setVisibility(View.GONE);
mTextSearch.setText("");
mEmptyText.setVisibility(View.GONE);
}
}
}
});
return view;
}
onCreate方法基本上有一堆clikListeners
setContentView(R.layout.main);
context = MainActivity.this;
Log.e("activityCreated", "ActivityCreated");
checkNewVersion();
Utils.sendTrackerDetails(this.getClass().getSimpleName());
AppRate.with(this)
.setInstallDays(1) // default 10, 0 means install day.
.setLaunchTimes(4) // default 10
.setRemindInterval(2) // default 1
.setShowLaterButton(true) // default true
.setDebug(false) // default false
.setOnClickButtonListener(new OnClickButtonListener() { // callback listener.
@Override
public void onClickButton(int which) {
Log.d(MainActivity.class.getName(), Integer.toString(which));
}
})
.monitor();
// Show a dialog if meets conditions
AppRate.showRateDialogIfMeetsConditions(this);
if (MainActivity.this.getResources().getBoolean(R.bool.portrait_only))
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
if (PreferenceManger.getInstance(MainActivity.this).getBooleanPreference(Constants.TAG_FIRST_USE)) {
if (isRooted() == true) {
Log.e("Device Rooted", "Device Rooted");
showAlert();
} else {
Log.e("Device UnRooted", "Device UnRooted");
}
PreferenceManger.getInstance(MainActivity.this).putBooleanPreference(Constants.TAG_FIRST_USE, false);
}
mClientSpaceButton = (RelativeLayout) findViewById(R.id.esapce_client_button);
mClientSpaceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!Application.ismIsConnected())
startActivity(new Intent(MainActivity.this, LoginActivity.class));
//switchActivityWithLoginVerified("");
//addFragment(new AuthentificationFragment(), null, AuthentificationFragment.class.getCanonicalName(), false, MainActivity.this);
else
startActivity(new Intent(MainActivity.this, CompteActivity.class));
}
});
mLayoutSimulationPlacement = (RelativeLayout) findViewById(R.id.simulation_placement_layout);
mLayoutSimulationPlacement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFragment(new SimulationPlaFragment(), null, SimulationPlaFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutReclamation = (RelativeLayout) findViewById(R.id.layout_reclamation);
mLayoutReclamation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
mLayoutAgence = (RelativeLayout) findViewById(R.id.layout_agence);
mLayoutAgence.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapFragment.Status = Constants.TAG_CLASS_AGENCE;
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(getRes(), MapFragment.newInstance(), "MapFragment");
Log.e("created", "fragment");
ft.commit();
}
});
mLayoutGAB = (RelativeLayout) findViewById(R.id.layout_gab);
mLayoutGAB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapFragment.Status = Constants.TAG_CLASS_DAB;
addFragment(new MapFragment(), null, MapFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutTmmHistory = (RelativeLayout) findViewById(R.id.tmm_layout);
mLayoutTmmHistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFragment(new TMMFragment(), null, TMMFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutSimulationCredit = (RelativeLayout) findViewById(R.id.simulation_credit);
mLayoutSimulationCredit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFragment(new SimulationCreditFragment(), null, SimulationCreditFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutTauxChange = (RelativeLayout) findViewById(R.id.layout_change);
mLayoutTauxChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFragment(new TauxChangeFragment(), null, TauxChangeFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutProduits = (RelativeLayout) findViewById(R.id.layout_produits);
mLayoutProduits.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFragment(new ListeProduitsFragment(), null, ListeProduitsFragment.class.getCanonicalName(), false, MainActivity.this);
}
});
mLayoutVisite = (RelativeLayout) findViewById(R.id.layout_visite);
mLayoutVisite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString(Constants.PDF_NAME, "visite_guidee.html");
addFragment(
new WebViewFragment(),
bundle,
WebViewFragment.class
.getCanonicalName(), true, MainActivity.this
);
}
});
}
答案 0 :(得分:0)
我的愚蠢错误,这是由于异步任务doInBackgroundMethod,发现一个catch块重新启动活动。