-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix background crash focus #11789
Open
Thompson3142
wants to merge
7
commits into
TeamNewPipe:dev
Choose a base branch
from
Thompson3142:fix_background_crash_focus
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+34
−8
Open
Fix background crash focus #11789
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
402f0b1
Initial commit for better handling of background crashes
Thompson3142 bdbdc29
Fix crashing behaviour with entry in SharedPreferences
Thompson3142 9ebae13
A few minor improvements
Thompson3142 0f0d610
Added docs for isInBackground
Thompson3142 2cf584b
Some more minor changes
Thompson3142 09e2f8f
Overwrite methods in MainActivity instead of creating a new class
Thompson3142 5dc48af
Remove no longer needed dependency
Thompson3142 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
app/src/main/java/org/schabi/newpipe/error/AppLifecycleObserver.kt
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,42 @@ | ||
package org.schabi.newpipe.error | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.util.Log | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.preference.PreferenceManager | ||
|
||
object AppLifecycleObserver : DefaultLifecycleObserver { | ||
private const val KEY_IS_IN_BACKGROUND = "is_in_background" | ||
private var TAG = javaClass.simpleName | ||
private lateinit var sharedPreferences: SharedPreferences | ||
private lateinit var editor: SharedPreferences.Editor | ||
|
||
// Only call this once on startup | ||
fun initialize(context: Context) { | ||
if (!this::sharedPreferences.isInitialized) { | ||
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) | ||
editor = sharedPreferences.edit() | ||
} | ||
} | ||
|
||
override fun onStart(owner: LifecycleOwner) { | ||
editor.putBoolean(KEY_IS_IN_BACKGROUND, false).commit() | ||
Log.d(TAG, "App moved to foreground") | ||
} | ||
|
||
override fun onPause(owner: LifecycleOwner) { | ||
editor.putBoolean(KEY_IS_IN_BACKGROUND, true).commit() | ||
Log.d(TAG, "App moved to background") | ||
} | ||
|
||
|
||
/** | ||
* Returns if the app is currently in the background | ||
* or in case of a crash the state when the crash happened | ||
*/ | ||
fun isInBackground(): Boolean { | ||
return sharedPreferences.getBoolean(KEY_IS_IN_BACKGROUND, true) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you override these functions directly in
MainActivity
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can, i just missed that AppCompatActivity has these methods to overwrite. Unfortunately it does not seem like it fixes the occasionally wrong behavior i mentioned, probably something related to ACRA but I am unsure how to fix it.