我有两个班级AdminPinActivity.kt
和AdminPinActivityPresenter.kt
AdminPinActivity.kt:
class AdminPinActivity : InjectableAppCompatActivity() {
@Inject
lateinit var adminPinActivityPresenter: AdminPinActivityPresenter
private var input_Pin: String = ""
private var input_Confirm_Pin: String = ""
companion object {
fun createAdminPinActivityIntent(context: Context, profileId: Int, colorRgb: Int): Intent {
val intent = Intent(context, AdminPinActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
intent.putExtra(KEY_ADMIN_PIN_PROFILE_ID, profileId)
intent.putExtra(KEY_ADMIN_PIN_COLOR_RGB, colorRgb)
return intent
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activityComponent.inject(this)
adminPinActivityPresenter.handleOnCreate(savedInstanceState)
}
override fun onSaveInstanceState(outState: Bundle) {
adminPinActivityPresenter.handleOnSavedInstanceState(outState)
super.onSaveInstanceState(outState)
}
override fun onSupportNavigateUp(): Boolean {
finish()
return false
}
}
AdminPinActivityPresenter.kt:
/** The presenter for [AdminPinActivity]. */
@ActivityScope
class AdminPinActivityPresenter @Inject constructor(
private val context: Context,
private val activity: AppCompatActivity,
private val profileManagementController: ProfileManagementController,
private val viewModelProvider: ViewModelProvider<AdminPinViewModel>
) {
private val adminViewModel by lazy {
getAdminPinViewModel()
}
private var input_Pin: String = ""
private var input_Confirm_Pin: String = ""
/** Binds ViewModel and sets up text and button listeners. */
fun handleOnCreate(savedInstanceState: Bundle?) {
activity.title = activity.getString(R.string.add_profile_title)
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)
activity.supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp)
val binding =
DataBindingUtil.setContentView<AdminPinActivityBinding>(activity, R.layout.admin_pin_activity)
binding.apply {
lifecycleOwner = activity
viewModel = adminViewModel
}
if (savedInstanceState != null) {
if (savedInstanceState.getString("InputPin") != null) {
Log.i("Input pin from savedInstanceState",savedInstanceState.getString("InputPin"))
binding.inputPin.input.setText(savedInstanceState.getString("InputPin"))
}
if (savedInstanceState.getString("InputConfirmPin") != null) {
binding.inputConfirmPin.input.setText(savedInstanceState.getString("InputConfirmPin"))
}
binding.executePendingBindings()
}
binding.inputPin.addTextChangedListener(object :TextWatcher{
override fun afterTextChanged(p0: Editable?) {}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
input_Pin=p0.toString().trim()
}
})
binding.inputConfirmPin.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
input_Confirm_Pin=p0.toString().trim()
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
})
}
fun handleOnSavedInstanceState(bundle: Bundle) {
bundle.putString("InputPin", input_Pin)
Log.i("input_Pin",input_Pin)
bundle.putString("InputConfirmPin", input_Confirm_Pin)
}
private fun getAdminPinViewModel(): AdminPinViewModel {
return viewModelProvider.getForActivity(activity, AdminPinViewModel::class.java)
}
}
Log.i("Input pin from savedInstanceState",savedInstanceState.getString("InputPin"))
和Log.i("input_Pin",input_Pin)
每次旋转都记录下来。这意味着代码可以正常工作,但是EditText值未得到更新,即binding.inputPin.input.setText(savedInstanceState.getString("InputPin"))
和binding.inputConfirmPin.input.setText(savedInstanceState.getString("InputConfirmPin"))
无法正常工作。
当我在if语句外(即handleOnCreateMethod内)使用binding.inputPin.input.setText("Something")
时,它将一次用“某些”更新EditText值。但是在屏幕旋转时也丢失了。
希望您能理解这个问题。
我到处搜索了将近两天,找不到解决方案。我不想使用双向数据绑定。
admin_pin_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:profile="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="org.oppia.app.profile.AdminPinViewModel" />
</data>
<ScrollView
android:id="@+id/scrollViewAdminPin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/addProfileBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/admin_auth_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/addProfileBackground">
<TextView
android:id="@+id/admin_pin_main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/admin_pin_requirement_description"
android:textColor="@color/oppiaPrimaryText"
android:textSize="16sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"/>
<TextView
android:id="@+id/admin_pin_warning_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/admin_pin_pin_description"
android:textSize="16sp"
android:textColor="@color/oppiaPrimaryText"
app:layout_constraintTop_toBottomOf="@+id/admin_pin_main_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"/>
<org.oppia.app.profile.ProfileInputView
android:id="@+id/input_pin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/admin_pin_warning_text"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:label="@string/admin_pin_new_pin"
app:isPasswordInput="true"
app:inputLength="5"
profile:error="@{viewModel.pinErrorMsg}"/>
<org.oppia.app.profile.ProfileInputView
android:id="@+id/input_confirm_pin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/input_pin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:label="@string/admin_pin_new_confirm"
app:isPasswordInput="true"
app:inputLength="5"
profile:error="@{viewModel.confirmPinErrorMsg}"/>
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/admin_pin_submit"
style="@style/StateButtonActive"
app:layout_constraintTop_toBottomOf="@+id/input_confirm_pin"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginEnd="32dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
profile_input_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="28dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical">
<TextView
android:id="@+id/label_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:fontFamily="sans-serif"
android:textAllCaps="true"
android:textColor="@color/oppiaPrimaryText"
android:textSize="12sp" />
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@drawable/add_profile_edit_text_background"
android:fontFamily="sans-serif"
android:padding="8dp"
android:textColor="@color/oppiaPrimaryText"
android:textSize="14sp"
android:textStyle="italic" />
<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red" />
</LinearLayout>
</layout>