Close activity android kotlin

Kotlin close app with dialog android code example

Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Solution 3: Have modified @user919216 code .. and made it compatible with WebView Solution 1: Solution 2: Use to achieve.

How to close a Dialog in Android programmatically?

You can call dismiss on the dialog.

This is an example of how to create a AlertDialog with 2 Buttons (OK and cancel). When clicking the cancel button,

is called to close the dialog.

From anywhere outside, you could call

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage("Some message.") .setPositiveButton("OK", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int id) < // do something >>) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int id) < dialog.dismiss(); >>); builder.show(); 

Only this line will close it. 🙂

Implement it in the onClickListener.

How to Close Alert Dialog Box in Android, Step 1: Create a New Project in Android Studio. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project. Step 2: Working with …

Читайте также:  Пример навыка алисы php

How to quit an android application programmatically using Kotlin?

This example demonstrates how to quit an android application programmatically using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Step 3 − Add the following code to MainActivity.kt

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import kotlin.system.exitProcess class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" >fun quitApp(view: View) < this@MainActivity.finish() exitProcess(0) >>

Step 4 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

Android Alert Dialog using Kotlin, If this method is set to false, you need to explicitly cancel the dialog using dialog.cancel() method. Alert Dialog Kotlin Code. To use AlertDialog in your Android Studio project, import the following class. import android.support.v7.app.AlertDialog; Following Kotlin code is used to create a …

How to show a dialog to confirm that the user wishes to exit an Android Activity?

In Android 2.0+ this would look like:

@Override public void onBackPressed() < new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Closing Activity") .setMessage("Are you sure you want to close this activity?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() < @Override public void onClick(DialogInterface dialog, int which) < finish(); >>) .setNegativeButton("No", null) .show(); > 

In earlier versions it would look like:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) < //Handle the back button if(keyCode == KeyEvent.KEYCODE_BACK) < //Ask the user if they want to quit new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle(R.string.quit) .setMessage(R.string.really_quit) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() < @Override public void onClick(DialogInterface dialog, int which) < //Stop the activity YourClass.this.finish(); >>) .setNegativeButton(R.string.no, null) .show(); return true; > else < return super.onKeyDown(keyCode, event); >> 
@Override public void onBackPressed() < new AlertDialog.Builder(this) .setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int id) < ExampleActivity.super.onBackPressed(); >>) .setNegativeButton("No", null) .show(); > 

Have modified @user919216 code .. and made it compatible with WebView

@Override public void onBackPressed() < if (webview.canGoBack()) < webview.goBack(); >else < AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int id) < finish(); >>) .setNegativeButton("No", new DialogInterface.OnClickListener() < public void onClick(DialogInterface dialog, int id) < dialog.cancel(); >>); AlertDialog alert = builder.create(); alert.show(); > > 

Android — Calling DialogFragment in Fragment Kotlin, A couple of suggestions: You can make you DialogFragment more kotliny by using the Kotlin standard functions. companion object < private const val KEY = "param1" @JvmStatic fun newInstance (param1: String) = ExampleDialogFragment ().apply < arguments = bundleOf (KEY to param1) >> You …

Android close dialog after 5 seconds?

final AlertDialog.Builder dialog = new AlertDialog.Builder(this).setTitle("Leaving launcher").setMessage("Are you sure you want to leave the launcher?"); dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() < @Override public void onClick(DialogInterface dialog, int whichButton) < exitLauncher(); >>); final AlertDialog alert = dialog.create(); alert.show(); // Hide after some seconds final Handler handler = new Handler(); final Runnable runnable = new Runnable() < @Override public void run() < if (alert.isShowing()) < alert.dismiss(); >> >; alert.setOnDismissListener(new DialogInterface.OnDismissListener() < @Override public void onDismiss(DialogInterface dialog) < handler.removeCallbacks(runnable); >>); handler.postDelayed(runnable, 10000); 

Use CountDownTimer to achieve.

 final AlertDialog.Builder dialog = new AlertDialog.Builder(this) .setTitle("Leaving launcher").setMessage( "Are you sure you want to leave the launcher?"); dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() < @Override public void onClick(DialogInterface dialog, int whichButton) < exitLauncher(); >>); final AlertDialog alert = dialog.create(); alert.show(); new CountDownTimer(5000, 1000) < @Override public void onTick(long millisUntilFinished) < // TODO Auto-generated method stub >@Override public void onFinish() < // TODO Auto-generated method stub alert.dismiss(); >>.start(); 

Late, but I thought this might be useful for anyone using RxJava in their application.

RxJava comes with an operator called .timer() which will create an Observable which will fire onNext() only once after a given duration of time and then call onComplete() . This is very useful and avoids having to create a Handler or Runnable.

More information on this operator can be found in the ReactiveX Documentation

// Wait afterDelay milliseconds before triggering call Subscription subscription = Observable .timer(5000, TimeUnit.MILLISECONDS) // 5000ms = 5s .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1() < @Override public void call(Long aLong) < // Remove your AlertDialog here >>); 

You can cancel behavior triggered by the timer by unsubscribing from the observable on a button click. So if the user manually closes the alert, call subscription.unsubscribe() and it has the effect of canceling the timer.

How to quit android application programmatically, This is the simplest method and works anywhere, quit the app for real, you can have a lot of activity opened will still quitting all with no problem. example on a button click public void exitAppCLICK (View view) < finishAffinity (); System.exit (0); >or if you want something nice, example with an alert dialog with …

Источник

How to close all Android activities at once using Kotlin?

This example demonstrates how to close all Android activities at once using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Step 3 − Add the following code to src/MainActivity.kt

package com.example.q28 import android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() < lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" button = findViewById(R.id.button) button.setOnClickListener < val intent = Intent(this@MainActivity, SecondActivity::class.java) startActivity(intent) >> >

Step 4 − Create a new activity and add the following code −

activity_second.xml −

SecondActivity.kt

import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity import kotlin.system.exitProcess class SecondActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) title = "KotlinApp" val button: Button = findViewById(R.id.terminateButton) button.setOnClickListener < finish() >> override fun onDestroy() < super.onDestroy() exitProcess(0) >>

Step 5 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen.

Click here to download the project code.

Источник

How to quit an android application programmatically using Kotlin?

This example demonstrates how to quit an android application programmatically using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Step 3 − Add the following code to MainActivity.kt

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import kotlin.system.exitProcess class MainActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" >fun quitApp(view: View) < this@MainActivity.finish() exitProcess(0) >>

Step 4 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

Источник

How to close all Android activities at once using Kotlin?

Solution: You can try Solution 1: In , set as true for Activity B, C and D. set it as false for Activity A (actually, the default is false). Demo: For the exit : Solution 2: Quite complicated solution, but allows to fully manipulate backstack.

How to close all Android activities at once using Kotlin?

This example demonstrates how to close all Android activities at once using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

Step 3 − Add the following code to src/MainActivity.kt

package com.example.q28 import android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() < lateinit var button: Button override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "KotlinApp" button = findViewById(R.id.button) button.setOnClickListener < val intent = Intent(this@MainActivity, SecondActivity::class.java) startActivity(intent) >> >

Step 4 − Create a new activity and add the following code −

activity_second.xml −

SecondActivity.kt

import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity import kotlin.system.exitProcess class SecondActivity : AppCompatActivity() < override fun onCreate(savedInstanceState: Bundle?) < super.onCreate(savedInstanceState) setContentView(R.layout.activity_second) title = "KotlinApp" val button: Button = findViewById(R.id.terminateButton) button.setOnClickListener < finish() >> override fun onDestroy() < super.onDestroy() exitProcess(0) >>

Step 5 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen.

Click here to download the project code.

How to finish multiple activities at once in android?, First create the instance of all activity using this code. Activity A < >Activity B < public static Activity activityB; oncreate () < activityB=this; >Activity C < public static Activity activityC; oncreate () < activityC=this; >Activity D < >2. Please call this method on back press in Activity D private finishActivity () < …

How can I close the whole Kotlin app when I click a button? [duplicate]

Android — How to call a function from button click kotlin, 0. There are 3 ways to do it in Kotlin: Using Object — As it is a static function which doesn’t access views or data of MainActivity, you can call it through an object of MainActivity as it need not be the running instance. So, you can call it as MainActivity ().triggerRestart ().

How to finish multiple activities at once in android?

In AndroidManifest.xml , set android:noHistory as true for Activity B, C and D. set it as false for Activity A (actually, the default is false).

exitBtn.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < Intent intent = new Intent(getApplicationContext(),ActivityB.class); startActivity(intent); finish(); >>); 

Quite complicated solution, but allows to fully manipulate backstack. Basically create your own «backstack» in you Application :

public class MyApplication extends Application < private SetrunningActivities = new HashSet<>(); onCreate() < // . registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() < @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) < runningActivities.add(activity); >@Override public void onActivityDestroyed(Activity activity) < runningActivities.remove(activity); >>); > public void killActivities(Set> activitiesToRemove) < for (Activity stackActivity : runningActivities) < if (activitiesToRemove.contains(stackActivity.getClass())) < stackActivity.finish(); >> > > 

Now you can call something like this in you ActivityD:

You can use Map instead of Set if class isn’t only specifier necessary to determine which activity should be removed.

Please corret me if there is something wrong with my solution, but it seems like there should be no leaks and no boilerplate in your activities

Edit — Kotlin version:

class MyApplication : Application() < private val runningActivities = mutableSetOf() override fun onCreate() < // . registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks < override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) < runningActivities.add(activity) >override fun onActivityDestroyed(activity: Activity) < runningActivities.remove(activity) >>) > fun killActivities(activitiesToRemove: Set) < runningActivities.forEach < activity ->if (activitiesToRemove.contains(activity.javaClass)) < activity.finish() >> > > 

Just add this in your ActivityD.class

 finishButton.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View v) < Intent intent = new Intent(getApplicationContext(),ActivityB.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); >>); 

Exit From App on Double Click of Back Button in Android, Step 3: Now in mainActivity, We will override onBackPressed () method. With the first press of the back button, we will store the current system time, and display a toast. If the user presses the back button again within 3 seconds we will call the finish () method. Kotlin. package com.ayush.gfg_exit.

Источник

Оцените статью