This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move hacky code in specific subclass (#46)
- Loading branch information
Showing
2 changed files
with
44 additions
and
22 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
lib/src/main/java/com/tbruyelle/rxpermissions/EnsureSameProcessActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters