how to get current activity from stack activity in Android

3.5k views Asked by At

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!!!

1

There are 1 answers

5
prashant17 On BEST ANSWER

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");`