

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to launch activity only once when Android app is opened for the first time?
This example demonstrates about How to launch activity only once when Android app is opened for the first time.
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.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="I will be launched once only" /> </LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
package app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; public class MainActivity extends AppCompatActivity { String prevStarted = "prevStarted"; @Override protected void onResume() { super.onResume(); SharedPreferences sharedpreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE); if (!sharedpreferences.getBoolean(prevStarted, false)) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putBoolean(prevStarted, Boolean.TRUE); editor.apply(); } else { finish(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 4 − Add the following code to androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
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 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.
- Related Questions & Answers
- How to launch another app from your app?
- How to call OnDestroy Activity in Android app?
- How to refresh Activity when IntentService is finished in android?
- Android Determine App Starts First Time OR Not Programmatically?
- Send a notification when the Android app is closed
- When was the word Hindu used for the first time?
- How to get android notifications when the app was closed?
- How to call OnDestroy Activity in an Android App using Kotlin?
- How to open last activity when tapping on Android notification?
- How to apply a theme to an activity only in Android?
- How to call an activity method from a fragment in Android App?
- How do I create a transparent Activity in an Android App?
- How to start an Android activity when use clicks on Notification?
- How to launch any arbitrary iPhone application from within another app?
- How to start an activity when user clicks a notification in Android?