Top activity name tutorial will help you to get name of top running activity name.
Steps to use Top Activity Name in your project.
Steps to use Top Activity Name in your project.
- Crate a project(TopActivityDemo) in android studio.
- Now update MainActivity.Java to-
package com.pankaj.topactivitynamedemo; import android.app.ActivityManager; import android.content.Context; import android.os.Build; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void getTopActivity(View view) { //Create the object of activity file manager ActivityManager mActivityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE); //get activity name String className = mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName(); //show activity name on toast Toast.makeText(this, className, Toast.LENGTH_SHORT).show(); } }
- Open your main_activity.xml file and update it-
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.pankaj.topactivitynamedemo.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Top Activity" android:onClick="getTopActivity" /> </android.support.constraint.ConstraintLayout>
- All the application development process with TopActivityName has completed, Now run the app and look the screen.
- Good bye, Thanks to read this blog.
No comments:
Post a Comment