Search code examples
androidandroid-activityandroid-contextactivity-stack

how to get current activity from stack activity in Android


I want to get current activity from activity stack and replace with ??????

dialog =new AlertDialog.Builder(??????,android.R.style.Theme_Material_Dialog_Alert).setTitle("Tittle").setMessage("Message");`

How Can I Do it?

Update1 :

I can't use getContext() or getActivty because they return null

Also I find this

ActivityManager am =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);

ComponentName cn = am.getRunningTasks(1).get(0).topActivity;

I dont know how to use cn to conver to a context or Activity ?

Please help me!!!


Solution

  • Try this

    Context context;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }
    

    then

    dialog =new AlertDialog.Builder(context,android.R.style.Theme_Material_Dialog_Alert).setTitle("Tittle").setMessage("Message");`