-
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.
Merge pull request #24 Improved Preference Screen from MrNegativeTW/p…
…reference-screen #6 Improved Preference Screen
- Loading branch information
Showing
12 changed files
with
257 additions
and
108 deletions.
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
66 changes: 46 additions & 20 deletions
66
course/app/src/main/java/com/txwstudio/app/timetable/Ui/activity/PreferenceActivity.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 |
---|---|---|
@@ -1,57 +1,83 @@ | ||
package com.txwstudio.app.timetable.ui.activity | ||
|
||
import android.os.Bundle | ||
import android.view.MenuItem | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.FragmentTransaction | ||
import androidx.fragment.app.commit | ||
import androidx.preference.Preference | ||
import androidx.preference.PreferenceFragmentCompat | ||
import com.txwstudio.app.timetable.R | ||
import com.txwstudio.app.timetable.databinding.ActivityPreferenceBinding | ||
import com.txwstudio.app.timetable.ui.preferences.PreferenceFragment | ||
|
||
private const val TITLE_TAG = "settingsActivityTitle" | ||
|
||
class PreferenceActivity : AppCompatActivity(), | ||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { | ||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { | ||
|
||
private lateinit var binding: ActivityPreferenceBinding | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_preference) | ||
|
||
setSupportActionBar(binding.toolbarSettingsAct) | ||
supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||
} | ||
binding.toolbarSettingsAct.setNavigationOnClickListener { | ||
onBackPressed() | ||
} | ||
|
||
if (savedInstanceState == null) { | ||
supportFragmentManager.beginTransaction() | ||
.replace(R.id.fragment_container_view, PreferenceFragment()) | ||
.commit() | ||
} else { | ||
title = savedInstanceState.getCharSequence(TITLE_TAG) | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
return when (item.itemId) { | ||
android.R.id.home -> { | ||
finish() | ||
true | ||
supportFragmentManager.addOnBackStackChangedListener { | ||
if (supportFragmentManager.backStackEntryCount == 0) { | ||
setTitle(R.string.title_activity_settings) | ||
} | ||
else -> super.onOptionsItemSelected(item) | ||
} | ||
} | ||
|
||
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat?, pref: Preference): Boolean { | ||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
// Save current activity title so we can set it again after a configuration change | ||
outState.putCharSequence(TITLE_TAG, title) | ||
} | ||
|
||
override fun onSupportNavigateUp(): Boolean { | ||
if (supportFragmentManager.popBackStackImmediate()) { | ||
return true | ||
} | ||
return super.onSupportNavigateUp() | ||
} | ||
|
||
override fun onPreferenceStartFragment( | ||
caller: PreferenceFragmentCompat, | ||
pref: Preference | ||
): Boolean { | ||
// Instantiate the new Fragment | ||
val args = pref.extras | ||
val fragment = supportFragmentManager.fragmentFactory.instantiate( | ||
classLoader, | ||
pref.fragment) | ||
fragment.arguments = args | ||
fragment.setTargetFragment(caller, 0) | ||
classLoader, | ||
pref.fragment | ||
).apply { | ||
arguments = args | ||
// setTargetFragment(caller, 0) // deprecated in java | ||
} | ||
|
||
// Replace the existing Fragment with the new Fragment | ||
supportFragmentManager.commit { | ||
setCustomAnimations( | ||
R.anim.fade_in, | ||
R.anim.fade_out, | ||
R.anim.fade_in, | ||
R.anim.fade_out | ||
) | ||
setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) | ||
replace(R.id.fragment_container_view, fragment) | ||
addToBackStack(null) | ||
} | ||
binding.appBarLayoutSettingsAct.setExpanded(true, true) | ||
title = pref.title | ||
return 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.