活动暂停被调用两次

时间:2016-03-21 12:49:21

标签: android

在我的应用程序代码中进行更改并且我的应用程序被重新编译后,我遇到了一个奇怪的问题... onPause活动生命周期方法在从最初运行大约20秒后第二次运行...有没有人遇到过类似的问题?

两次调用onPause的活动:

public class LoginActivity extends AppCompatActivity implements NetworkStateListeners, LoginActivityCommonElementsAndMuchMore {

private static final String debugTag = LoginActivity.class.getSimpleName();
private SpannableStringBuilder spannableStringBuilder;
private StyleSpan styleSpan;
private SignInFragment signInFragment;
private RegisterFragment registerFragment;
private ForgotPasswordFragment forgotPasswordFgmt;
private ClickableSpan clickableSpan;
private Bundle loginActivityBundle;
private int connectionStatus;
private NetworkState networkState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e(debugTag, "onCreate");
    setContentView(R.layout.login_activity);

    if ( savedInstanceState == null ) {
        networkState        =   new NetworkState();
        loginActivityBundle =   new Bundle();

        signInFragment = new SignInFragment();
        signInFragment.setArguments(loginActivityBundle);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_container, signInFragment, getResources().getString(R.string.signInFgmt))
                .commit();

        getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                Log.d(debugTag, "BACK STACK CHANGED!");
                Log.d(debugTag, "BACK STACK COUNT: " + getSupportFragmentManager().getBackStackEntryCount());
                for (int i = 0; i < getSupportFragmentManager().getBackStackEntryCount(); i++) {
                    Log.d(debugTag, "BACK STACK ENTRIES: " + getSupportFragmentManager().getBackStackEntryAt(i).getName());
                }
            }
        });
    }
}

@Override
protected void onResume() {
    super.onResume();
    Log.e(debugTag, "onResume");
    networkState.addListener(this);
    registerReceiver(networkState, new IntentFilter(getResources().getString(R.string.connectivity_change)));
}

@Override
protected void onPause() {
    super.onPause();
    Log.e(debugTag, "onPause");
    networkState.removeListener(this);
    unregisterReceiver(networkState);
    int x = 10;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    RefWatcher refWatcher = MyApplication.getRefWatcher(this);
    refWatcher.watch(this);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    if ( getSupportFragmentManager().getBackStackEntryCount() > 0 ) getSupportFragmentManager().popBackStack(0, 0);
}

@Override
public void setLoginActivitySpan(TextView textView, String stringResource, int start, int end, final int code) {
    spannableStringBuilder = new SpannableStringBuilder(stringResource);
    styleSpan = new StyleSpan(Typeface.BOLD);
    clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {

        }
    };
    spannableStringBuilder.setSpan(clickableSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.setSpan(styleSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    spannableStringBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getApplicationContext(), R.color.loginSpanColor)), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    textView.setText(spannableStringBuilder);
}

@Override
public void forgotPasswordOnClick() {
    forgotPasswordFgmt = new ForgotPasswordFragment();
    forgotPasswordFgmt.setArguments(loginActivityBundle);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_container, forgotPasswordFgmt, getResources().getString(R.string.forgotPasswordFgmt))
            .addToBackStack(getResources().getString(R.string.forgotPasswordFgmt))
            .commit();
}

@Override
public void registerOnClick() {
    registerFragment = new RegisterFragment();
    if ( getSupportFragmentManager().getBackStackEntryCount() > 0 ) getSupportFragmentManager().popBackStack();
    loginActivityBundle.putInt("connectivityStatus", connectionStatus);
    registerFragment.setArguments(loginActivityBundle);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_container, registerFragment, getResources().getString(R.string.registerFgmt))
            .addToBackStack(getResources().getString(R.string.registerFgmt))
            .commit();
}

@Override
public void signInHereOnClick() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) getSupportFragmentManager().popBackStack();
}

public static SharedPreferences getSessionPrefs(Context context) {
    //Mode private so only this app can modify this SharedPreferences file
    SharedPreferences sessionPrefs = context.getSharedPreferences(AppConfig.SESSION_PREFS, Context.MODE_PRIVATE);
    return sessionPrefs;
}

@Override
public boolean validateEditText(EditText[] fields) {
    for (EditText field : fields) {
        if (field.getText().toString().isEmpty() && field.getTag().equals(getResources().getString(R.string.required))) return false;
    }
    return true;
}

@Override
public String encodeUtf8(String text) {
    try {
        byte[] data = text.getBytes("UTF-8");
        return Base64.encodeToString(data, Base64.DEFAULT);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public String decodeUtf8(String text) {
    try {
        byte[] data = Base64.decode(text, Base64.DEFAULT);
        return new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public void networkStatus(int connectionType) {
    Log.e(debugTag, "listeners: "+connectionType);
    connectionStatus = connectionType;
    Intent intent = new Intent(getResources().getString(R.string.network_state_update));
    intent.putExtra(getResources().getString(R.string.connectivity_status), connectionStatus);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}

}

logcat的:

   03-21 15:10:35.842 20756-20756/com.votingsystem.tsiro.votingsystem        E/LoginActivity: onCreate
03-21 15:10:36.062 20756-20756/com.votingsystem.tsiro.votingsystem E/LoginActivity: onResume
03-21 15:10:36.082 20756-20851/com.votingsystem.tsiro.votingsystem D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-21 15:10:36.092 20756-20756/com.votingsystem.tsiro.votingsystem E/com.votingsystem.tsiro.broadcastReceivers.NetworkState: onReceive
03-21 15:10:36.092 20756-20756/com.votingsystem.tsiro.votingsystem E/com.votingsystem.tsiro.broadcastReceivers.NetworkState: notifyState Called
03-21 15:10:36.092 20756-20756/com.votingsystem.tsiro.votingsystem E/LoginActivity: listeners: 0
03-21 15:10:36.102 20756-20756/com.votingsystem.tsiro.votingsystem D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
03-21 15:10:36.102 20756-20756/com.votingsystem.tsiro.votingsystem D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
03-21 15:10:36.132 20756-20756/com.votingsystem.tsiro.votingsystem D/SRIB_DCS: log_dcs ThreadedRenderer::initialize entered! 
03-21 15:10:36.142 20756-20851/com.votingsystem.tsiro.votingsystem I/Adreno: EGLInit: QTI Build: 06/19/15, 9723ec1, I4ea93f0e25
03-21 15:10:36.152 20756-20851/com.votingsystem.tsiro.votingsystem I/OpenGLRenderer: Initialized EGL, version 1.4
03-21 15:10:36.162 20756-20851/com.votingsystem.tsiro.votingsystem D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
03-21 15:10:36.162 20756-20851/com.votingsystem.tsiro.votingsystem D/OpenGLRenderer: Enabling debug mode 0
03-21 15:10:36.442 20756-20756/com.votingsystem.tsiro.votingsystem I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@32d079c3 time:682299231
03-21 15:10:46.112 20756-20756/com.votingsystem.tsiro.votingsystem E/LoginActivity: onPause
03-21 15:10:46.322 20756-20756/com.votingsystem.tsiro.votingsystem E/LoginActivity: onResume
03-21 15:10:46.352 20756-20756/com.votingsystem.tsiro.votingsystem E/com.votingsystem.tsiro.broadcastReceivers.NetworkState: onReceive
03-21 15:10:46.352 20756-20756/com.votingsystem.tsiro.votingsystem E/com.votingsystem.tsiro.broadcastReceivers.NetworkState: notifyState Called
03-21 15:10:46.352 20756-20756/com.votingsystem.tsiro.votingsystem E/LoginActivity: listeners: 0
03-21 15:10:46.352 20756-20756/com.votingsystem.tsiro.votingsystem I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@32d079c3 time:682309143

0 个答案:

没有答案