--> android source code full: Top Activity Name | Deskripsi Singkat Blog di Sini

Find The Complete Source Code Android


Showing posts with label Top Activity Name. Show all posts
Showing posts with label Top Activity Name. Show all posts

Sunday, December 3, 2017

no image

Top Activity Name

Top activity name tutorial will help you to get name of top running activity name.




Steps to use Top Activity Name in your project.

  1. Crate a project(TopActivityDemo) in android studio.

  2. 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();
    }
    }



  3. Open your main_activity.xml file and update it-
  4. <?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>


  5. All the application development process with TopActivityName has completed, Now run the app and look the screen. 


  6. Good bye, Thanks to read this blog.