'Prevent dialog from closing after restarting application
I am implementing a dialog and have a question. I set a timer for 10minutes. For 10 minutes, users cannot use the application. They can only see a dialog that tells them how long it is left. When they terminate and restart the application, they can only see the same dialog. How can I prevent a dialog from closing or show it again when a user restarts the application?
Thanks for the help.
My layout
<?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:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.OtpTimeLimitViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_information_button">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layout_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="15dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="message1"
android:textColor="@android:color/white"
android:textSize="@dimen/text_size_tab_bar"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:text="message2"
android:textColor="@color/dark_gray"
android:textSize="@dimen/text_size_helper"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_text" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/timeLimit_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@{viewModel.timeLimit}"
android:textColor="@color/white"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/content_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Fragment
class OtpTimeLimitFragment : DialogFragment() {
private lateinit var binding: DialogOtpLimitBinding
private lateinit var viewModel: OtpTimeLimitViewModel
private lateinit var mContext: MainActivity
private val args : AgreementViewDialogArgs by navArgs()
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context as MainActivity
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.dialog_otp_limit, container, false)
dialog?.setCanceledOnTouchOutside(false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
viewModel = ViewModelProvider(this).get(OtpTimeLimitViewModel::class.java)
binding.viewModel = viewModel
}
}
Solution 1:[1]
Here's a rough example of what you need to do :
First of all, Store value like this :
var pref: SharedPreferences? = requireContext().getSharedPreferences("otpTrialLimit", MODE_PRIVATE)
pref?.edit()?.putLong("endTime", endTime)
pref?.edit()?.commit()
When user reopens the application then in
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
viewModel = ViewModelProvider(this).get(OtpTimeLimitViewModel::class.java)
binding.viewModel = viewModel
val sharedPreference = getSharedPreferences("otpTrialLimit",Context.MODE_PRIVATE)
sharedPreference.getLong("endTime", 0)
if(System.currentTimeMillis() < endTime){
//Show dialog here
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karan Mehta |
