forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add SingleFragmentActivity class
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
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
66 changes: 66 additions & 0 deletions
66
AnkiDroid/src/main/java/com/ichi2/anki/SingleFragmentActivity.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,66 @@ | ||
/* | ||
* Copyright (c) 2023 Brayan Oliveira <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.ichi2.anki | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentContainerView | ||
import androidx.fragment.app.commit | ||
import com.ichi2.themes.setTransparentStatusBar | ||
import com.ichi2.utils.getInstanceFromClassName | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.jvm.jvmName | ||
|
||
/** | ||
* Activity aimed to host a fragment on the entire screen. | ||
* For that, it uses [R.layout.single_fragment_activity], which has only a [FragmentContainerView] | ||
* | ||
* Useful to avoid creating a Activity for every new screen | ||
* while being able to reuse the fragment on other places. | ||
* | ||
* [getIntent] can be used as an easy way to build a [SingleFragmentActivity] | ||
*/ | ||
class SingleFragmentActivity : AnkiActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
if (showedActivityFailedScreen(savedInstanceState)) { | ||
return | ||
} | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.single_fragment_activity) | ||
setTransparentStatusBar() | ||
|
||
val fragmentClassName = requireNotNull(intent.getStringExtra(FRAGMENT_NAME_EXTRA)) { "'fragmentName' should be provided" } | ||
val fragment = getInstanceFromClassName<Fragment>(fragmentClassName).apply { | ||
arguments = intent.getBundleExtra(FRAGMENT_ARGS_EXTRA) | ||
} | ||
supportFragmentManager.commit { | ||
replace(R.id.fragment_container, fragment) | ||
} | ||
} | ||
companion object { | ||
private const val FRAGMENT_NAME_EXTRA = "fragmentName" | ||
private const val FRAGMENT_ARGS_EXTRA = "fragmentArgs" | ||
|
||
fun getIntent(context: Context, fragmentClass: KClass<out Fragment>, arguments: Bundle? = null): Intent { | ||
return Intent(context, SingleFragmentActivity::class.java).apply { | ||
putExtra(FRAGMENT_NAME_EXTRA, fragmentClass.jvmName) | ||
putExtra(FRAGMENT_ARGS_EXTRA, arguments) | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
AnkiDroid/src/main/res/layout/single_fragment_activity.xml
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,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:id="@+id/root_layout" | ||
tools:context=".SingleFragmentActivity"> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/fragment_container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> |
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