Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Move hacky code in specific subclass (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbruyelle committed Oct 16, 2016
1 parent 459536a commit 5d46850
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
@@ -0,0 +1,43 @@
package com.tbruyelle.rxpermissions;

import android.app.Activity;
import android.os.Bundle;
import android.os.Process;

/**
* In case of restore, ensures it's done by the same process, if not, kill self.
* </p>
* The goal is to prevent a crash when the activity is restored during a permission request
* but by another process. In that specific case the library is not able to restore the observable
* chain. This is a hack to prevent the crash, not a fix.
* </p>
* See https://github.com/tbruyelle/RxPermissions/issues/46.
*/
public abstract class EnsureSameProcessActivity extends Activity {
private static final String KEY_ORIGINAL_PID = "key_original_pid";

private int mOriginalProcessId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
mOriginalProcessId = Process.myPid();
} else {
mOriginalProcessId = savedInstanceState.getInt(KEY_ORIGINAL_PID, mOriginalProcessId);

boolean restoredInAnotherProcess = mOriginalProcessId != Process.myPid();

if (restoredInAnotherProcess) {
finish();
}
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putInt(KEY_ORIGINAL_PID, mOriginalProcessId);
}
}
Expand Up @@ -8,26 +8,12 @@
import android.os.Process;

@TargetApi(Build.VERSION_CODES.M)
public class ShadowActivity extends Activity {
private static final String KEY_ORIGINAL_PID = "key_original_pid";

private int mOriginalProcessId;

public class ShadowActivity extends EnsureSameProcessActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
handleIntent(getIntent());

mOriginalProcessId = Process.myPid();
} else {
mOriginalProcessId = savedInstanceState.getInt(KEY_ORIGINAL_PID, mOriginalProcessId);

boolean restoredInAnotherProcess = mOriginalProcessId != Process.myPid();

if(restoredInAnotherProcess) {
finish();
}
}
}

Expand All @@ -46,11 +32,4 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
RxPermissions.getInstance(this).onRequestPermissionsResult(requestCode, permissions, grantResults);
finish();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putInt(KEY_ORIGINAL_PID, mOriginalProcessId);
}
}

0 comments on commit 5d46850

Please sign in to comment.