简体   繁体   中英

Android not responding on pausing/resuming

I am keep on getting ANR alert in my application. This occurs when some other applications opened on top of my application and when that application closed I am getting ANR from my application. I am not doing any network operations or any looping in my UI thread. Not getting any log trace also. Below is my code.

Activity class:

public class AuthenticationActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_authentication);
    setupActionbar();
    String auth_page;

    if(getIntent().getExtras() != null)
        auth_page = getIntent().getExtras().getString("auth_page");
    else
        auth_page = "login";

    if(auth_page != null) {
        if (auth_page.equals("login")){
            getSupportFragmentManager().beginTransaction().replace(R.id.authentication_container, new LoginFragment()).commitAllowingStateLoss();
        }else if(auth_page.equals("signup")){
            getSupportFragmentManager().beginTransaction().replace(R.id.authentication_container, new RegisterationFragment()).commitAllowingStateLoss();

        }
    }
}

/**
 * Function to set the ToolBar as ActionBar
 */
private void setupActionbar() {
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    setTitle("");
    if(getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(IconUtils.getBackActionBarIcon(AuthenticationActivity.this));
    }

}


}

Fragment Class:

public class LoginFragment extends Fragment{

private EditText edtMobile;
private EditText edtPassword;
private TextView txtForgotPassword;
private Activity mActivity;
private MenuItem mMemuLogin;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    setHasOptionsMenu(true);
    mActivity = (Activity) context;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View page = inflater.inflate(R.layout.fragment_login, container, false);
    findViews(page);
    return page;
}


//preparing and calling the login service
private void login() {
    // Login service call
}

//initialize the views
private void findViews(View page) {

}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    mMemuLogin = menu.findItem(R.id.action_authentication_done).setTitle("Ride");
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_authentication_done) {
        login();
    }
    return super.onOptionsItemSelected(item);
}

}

The following dependencies are used in my application

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'net.steamcrafted:materialiconlib:1.0.3'
compile files('libs/volley.jar')
compile 'com.drivemode:TypefaceHelper:1.1.0'
compile 'com.android.support:multidex:1.0.1'
compile('com.crashlytics.sdk.android:crashlytics:2.5.7@aar') {
    transitive = true;
}
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services:9.0.2'
compile 'com.pubnub:pubnub-android-debug:3.7.10'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'joda-time:joda-time:2.3'
compile 'me.dm7.barcodescanner:zbar:1.8.4'
compile 'com.google.firebase:firebase-messaging:9.0.2'
compile 'com.facebook.android:facebook-android-sdk:4.13.0'

} apply plugin: 'com.google.gms.google-services'

Services used in my application manifest

<!-- [START firebase_service] -->
    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_service] -->
    <!-- [START firebase_iid_service] -->
    <service
        android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
    <!-- [END firebase_iid_service] -->

Application class

public class MyApplication extends Application  {
/**
 * Global request queue for Volley
 */
private RequestQueue mRequestQueue;
/**
 * A singleton instance of the application class for easy access in other places
 */
private static MyApplication sInstance;

public static final String TAG = MyApplication.class.getSimpleName ();
private Context mContext;

@Override
public void onCreate () {
    super.onCreate ();

    AnalyticsTrackers.initialize(this);
    AnalyticsTrackers.getInstance().get();
    Fabric.with(this, new Crashlytics());
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    FacebookSdk.setIsDebugEnabled(true);
    FacebookSdk.addLoggingBehavior(LoggingBehavior.APP_EVENTS);

    sInstance = this;
    mContext=this;
    TypefaceHelper.initialize(this);
}

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

@Override
public void onTerminate () {
    TypefaceHelper.destroy();
    super.onTerminate ();
}

/**
 * @return ApplicationController singleton instance
 */
public static synchronized MyApplication getInstance () {
    return sInstance;
}

public RequestQueue getRequestQueue () {
    // lazy initialize the request queue, the queue instance will be
    // created when it is accessed for the first time
    if(mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

}

Note : Found a log trace in play store

ANR Input dispatching timed out (Waiting to send non-key event because th...

How can I solve this. Do let me know if you need the full log trace.

I found the solution. I issue is with the google play services dependencies. It seems there is some problem in the dependence compile 'com.google.android.gms:play-services:9.0.2' . Removing this and adding appropriate dependencies solved the issue. In my case I added the following dependencies

com.google.android.gms:play-services-maps:9.0.2

com.google.android.gms:play-services-location:9.0.2
com.google.android.gms:play-services-analytics:9.0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM