How to Make Android App with Restart Button - Android Studio 2.2.2 Tutorial

Restart Android App with a button. It's very easy! Some times we had to switch off and then turn our applications on again, this can be easily done through the method that we're about to discuss on.



Here is our own tutorial with complete vocal explanation from our YouTube Channel




Manually doing this? Download the full source code and add this to your project files.


How to manually do this?

MainActivity.Java 
 package com.sabithpkcmnr.sampleapp;  

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button bt_restart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt_restart=(Button)findViewById(R.id.restart);
bt_restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent restartIntent = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(restartIntent);
}
});

}
}




activity_main
 <?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@android:color/white"
tools:context="com.sabithpkcmnr.sampleapp.MainActivity">

<Button
android:text="Restart This App"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restart"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


</RelativeLayout>


We have added button named restart with id as restart in xml file

Finally, run your app there you may find your app restart when you press the restart button.


Hope that was helpful!

Thanks!

1 comment:

  1. Please the video has been removed from YouTube ,can you send to nokolisticworld@gmail.com .please

    ReplyDelete

Powered by Blogger.