How to Call Oncreate Again Android
- Selected Reading
- UPSC IAS Exams Notes
- Developer'due south Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reload activeness in Android?
In some situations, we need to recall activeness again from onCreate(). This instance demonstrates how to reload activity in Android.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Projection and fill all required details to create a new project.
Footstep 2 − Add the following code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-eight"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:id = "@+id/parent" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:gravity = "heart" android:orientation = "vertical"> <TextView android:id = "@+id/text" android:textSize = "28sp" android:textAlignment = "center" android:layout_width = "match_parent" android:layout_height = "wrap_content" /> </LinearLayout>
In the above lawmaking, We have taken text view, when a user clicks on text view, it will call Main Activity again.
Footstep 3 − Add the post-obit code to src/MainActivity.java
package com.example.andy.myapplication; import android.content.Intent; import android.os.Build; import android.bone.Bundle; import android.support.annotation.RequiresApi; import android.back up.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; public course MainActivity extends AppCompatActivity { int view = R.layout.activity_main; TextView textview; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(view); textview = findViewById(R.id.text); textview.setText("Click here to recall activeness"); textview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View five) { Intent i = new Intent(MainActivity.this, MainActivity.course); finish(); overridePendingTransition(0, 0); startActivity(i); overridePendingTransition(0, 0); } }); } }
In the above code, we accept used Intent to recreate an activity as shown below -
Intent i = new Intent(MainActivity.this, MainActivity.form); cease(); overridePendingTransition(0, 0); startActivity(i); overridePendingTransition(0, 0);
In the to a higher place code, nosotros have used overridePendingTransition(), information technology is used to remove activity create animation while re-creating activity.
Let's endeavor to run your application. I assume you accept connected your actual Android Mobile device with your calculator. To run the app from android studio, open one of your projection's activity files and click Run icon from the toolbar. Select your mobile device equally an option and so check your mobile device which will display your default screen −
In the above result, it has shown default screen when you click on text view, it volition re-create Main Activeness from onCreate() as shown below -
Click here to download the project code
Published on 30-Jan-2019 11:35:12
- Related Questions & Answers
- How to set ringtone in Android from Android activity?
- How to restart an Activity in Android?
- How to pass an paradigm from one activity another activity in Android?
- How to create blur activeness background in android?
- How to get activity thumbnail size in android?
- How to get current action name in android?
- How to print current activity name in android?
- How to get full screen activeness in android?
- How to call OnDestroy Activity in Android app?
- How to Go back to previous activity in android
- How to send data to previous activity in Android?
- How to pass a String from one Action to some other Activeness in Android using Kotlin?
- How to get current activity available memory in android?
- How to become current land of activity in android?
- How to become current foreground activity context in Android?
Source: https://www.tutorialspoint.com/how-to-reload-activity-in-android
0 Response to "How to Call Oncreate Again Android"
Post a Comment