Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- val disposable = CompositeDisposable()
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- button.setOnClickListener {
- val f = TestPopup()
- usingRxJava(f)
- //usingLiveData(f)
- }
- }
- private fun usingRxJava(f: TestPopup) {
- val subject = SingleSubject.create<String>()
- f.show(supportFragmentManager, "TAG")
- button.post {
- f.dialog.setOnDismissListener {
- val str = f.arguments?.getString(TestPopup.TEST_KEY) ?: ""
- subject.onSuccess(str)
- }
- }
- subject.subscribe({
- Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
- }, {
- }).addTo(disposable)
- }
- private fun usingLiveData(f: TestPopup) {
- val liveData = MutableLiveData<String>()
- f.show(supportFragmentManager, "TAG")
- button.post {
- f.dialog.setOnDismissListener {
- val str = f.arguments?.getString(TestPopup.TEST_KEY) ?: ""
- liveData.postValue(str)
- }
- }
- liveData.observe(this, Observer {
- Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
- })
- }
- override fun onDestroy() {
- disposable.dispose()
- super.onDestroy()
- }
- }
- class TestPopup : DialogFragment() {
- override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
- return inflater.inflate(R.layout.dialog_test, container, false)
- }
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
- button_test.setOnClickListener {
- val arg = Bundle()
- arg.putString(TEST_KEY, edit_test.text.toString())
- arguments = arg
- dismiss()
- }
- }
- companion object {
- const val TEST_KEY = "KEY"
- }
- }
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
- button_test.setOnClickListener {
- val input = edit_test.text.toString()
- (activity as MyListener).inputComplete(input)
- dismiss()
- }
- }
- class MainActivity : AppCompatActivity(), TestPopup.MyListener {
- override fun inputComplete(input: String) {
- Toast.makeText(this, "Accept : $input", Toast.LENGTH_SHORT).show()
- }
- }
- //MainViewModel.kt
- class MainViewModel: ViewModel() {
- val dialogText = PublishProcessor.create<String>()
- fun postNewDialogText(text: String) {
- dialogText.onNext(text)
- }
- }
- // Activity
- val disposable = CompositeDisposable()
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- val viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)
- viewModel.dialogText.subscribe {
- Toast.makeText(this, "Accept : $it", Toast.LENGTH_SHORT).show()
- }.addTo(disposable)
- button.setOnClickListener {
- TestPopup().show(supportFragmentManager, "TAG")
- // usingRxJava(f)
- // usingLiveData(f)
- }
- }
- override fun onDestroy() {
- disposable.dispose()
- super.onDestroy()
- }
- // Dialog Fragment
- override fun onActivityCreated(savedInstanceState: Bundle?) {
- super.onActivityCreated(savedInstanceState)
- // Important!! use activity when getting the viewmodel.
- val viewModel = ViewModelProviders.of(requireActivity()).get(MainViewModel::class.java)
- button_test.setOnClickListener {
- viewModel.postNewDialogText(edit_test.text.toString())
- dismiss()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement