我有一个导航抽屉活动。在onCreate,我使用片段事务创建了一个MapFragment。
Fragment fragment = null;
String newttitl = "TEST";
fragment = new MapFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment, "MAIN");
fragmentTransaction.addToBackStack("MAIN");
fragmentTransaction.commit();
getSupportActionBar().setTitle(newttitl);
然后我使用Intent对抽屉物品进行了一些操作。对于每个项目我使用单独的意图。我有几个要加载的活动。但是我使用了一个简单的活动来加载。当我打电话给 onBackpressed 来完成我调用的活动时,它有时会给出ANR。请帮我解决这个问题。我不知道如何处理onPause,onResume。
NaviagtionActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
//checkConnection();
boolean isConnected = Connection.isConnected();
// Check if user is already logged in or not
Fragment fragment = null;
String newttitl = "FUEL ON";
fragment = MapFragment.newInstance();
if (fragment != null) {
checkConnection();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment, "MAIN");
fragmentTransaction.addToBackStack("MAIN");
fragmentTransaction.commit();
getSupportActionBar().setTitle(newttitl);
}
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
// display the first navigation drawer view on app launch
//displayView(0);
}
@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) {
Intent intent = new Intent(this, SettingActivity.class);
startActivity(intent);
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
Activity activity= null;
String title = getString(R.string.app_name);
String TAG=null;
boolean isConnected = Connection.isConnected();
switch (position) {
case 0:
// Check if user is already logged in or not
fragment = getSupportFragmentManager().findFragmentByTag("MAIN");
if (fragment != null && !fragment.isVisible()) {
// add your code here
TAG="MAIN";
fragment = new MapFragment();
title = "FUEL ON";
}
else
{
break;
}
break;
case 1:
//Launch and/or Bind to service here
Intent intent = new Intent(this,CheckActivity.class);
startActivity(intent);
break;
case 2:
intent= new Intent(this,SettingActivity.class);
startActivity(intent);
break;
case 3:
intent = new Intent(this, Sample.class);
startActivity(intent);
break;
case 4:
// Check if user is already logged in or not
intent = new Intent(this, Sample5.class);
startActivity(intent);
break;
case 5:
intent = new Intent(this, NotificationFragment.class);
startActivity(intent);
break;
case 6:
intent= new Intent(this,Sample3.class);
startActivity(intent);
break;
case 7:
intent = new Intent(this, Sample2.class);
startActivity(intent);
break;
case 8:
intent = new Intent(this, HelpFragment.class);
startActivity(intent);
break;
case 9:
intent = new Intent(this, AboutFragment.class);
startActivity(intent);
break;
default:
break;
}
if(isConnected) {
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment, TAG);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.addToBackStack(TAG);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
else {showSnack(false);}
}
@Override
public void onResume() {
// Connection connection = new Connection();
super.onResume();
Thread.currentThread().getState();
Log.e("NavigationResume", String.valueOf(Thread.currentThread())+Thread.currentThread().getState());
// Toast.makeText(this,"NAV"+"onResume"+Thread.currentThread().getState(),Toast.LENGTH_SHORT).show();
// register connection status listener
// MyApplication.getInstance().setConnectivityListener(this);
// LocalBroadcastManager.getInstance(this).registerReceiver();
}
@Override
public void onPause()
{
super.onPause();
// this.unregisterReceiver(connection);
Log.e("NavigationPause", String.valueOf(Thread.currentThread())+Thread.currentThread().getState());
// Toast.makeText(this,"NAV"+"onPause",Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);
alertDialog.setMessage("Do you want to exit ?");
alertDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// firstName.setEnabled(true);
// lastName.setEnabled(true);
// mobNum.setEnabled(true);
// emailAd.setEnabled(true);
finish();
// Write your code here to invoke YES event
}
});
alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
}
});
alertDialog.show();
}
@Override
public void onNetworkConnectionChanged(boolean isConnected) {
showSnack(isConnected);
}
}
我的MapFragment。
package com.example.administrator.login.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.administrator.login.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* Created by Administrator on 03-08-2016.
*/
public class MapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
View rootView;
// Connection detector class
public MapFragment () {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
rootView = inflater.inflate(R.layout.activity_maps, container, false);
SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map));
// toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mapFragment.getMapAsync(this);
return rootView;
}
@Override
public void onPause()
{
super.onPause();
Toast.makeText(getContext(),getContext().toString()+"Pause",Toast.LENGTH_SHORT).show();
//getActivity().getSupportFragmentManager().popBackStack();
}
@Override
public void onResume()
{
super.onResume();
Toast.makeText(getContext(),getContext().toString()+"Resume",Toast.LENGTH_SHORT).show();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
我的意图
public class Sample extends AppCompatActivity {
private Button applyCode,addCode;
private EditText editPromocode;
private CardView promocardView;
ProgressDialog pDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.promotions_fragment);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
super.onSaveInstanceState(outState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// navigateUpTo(new Intent(this,NavigationActivity.class));
super.onBackPressed();
PromotionFragment.this.finish();
return true;
}
@Override
public void onBackPressed()
{
Log.e("Hiii Thread", String.valueOf(Thread.interrupted()));
Thread.interrupted();
super.onBackPressed();
super.finish();
// new Thread() {
// @Override
// public void run() {
// //If there are stories, add them to the table
// try {
// // code runs in a thread
// PromotionFragment.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
//
// Toast.makeText(getApplicationContext(),"this is PUI thread",Toast.LENGTH_SHORT).show();
// Log.e("PromoUI", String.valueOf(Thread.currentThread())+Thread.currentThread().getPriority());
// finish();
// }
// });
// } catch (final Exception ex) {
// Log.i("---","Exception in thread");
// }
// }
// }.start();
}
@Override
protected void onStop() {
super.onStop();
//Toast.makeText(this,"Promotion"+"onStop",Toast.LENGTH_SHORT).show();
Log.e("PROMOSTOP", String.valueOf(Thread.currentThread()));
super.finish();
}
@Override
protected void onPause()
{
super.onPause();
// super.finish();
// Toast.makeText(this,this.toString()+"onPause",Toast.LENGTH_SHORT).show();
//finish();
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("PROMODESTROY", String.valueOf(Thread.currentThread()));
this.finish();
super.finish();
// Fragment fragment=null;
// fragment = MapFragment.newInstance();
// if (fragment != null) {
//
// FragmentManager fragmentManager = getSupportFragmentManager();
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.remove(fragment);
// fragmentTransaction.commit();
//
// }
// Toast.makeText(this,this.toString()+"Destroy",Toast.LENGTH_SHORT).show();
}