-1

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

Community
  • 1
  • 1
Amir133
  • 2,372
  • 2
  • 18
  • 34

1 Answers1

0

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");`
prashant17
  • 1,520
  • 3
  • 14
  • 23
  • Thanks for Answer but its may change my activity before execute `dialog =new AlertDialog.Builder...` – Amir133 Jul 22 '18 at 15:38
  • Yes your Answer is Correct but `dialog` show with some delay So The user can click on back button to go back to last Activity(So application may crash) – Amir133 Jul 22 '18 at 15:44
  • it show this Error `Unable to add window -- token android.os.BinderProxy@ee76728 is not valid; is your activity running?` – Amir133 Jul 22 '18 at 16:22
  • You need to figure out why your dialog causing a delay while launching – prashant17 Jul 23 '18 at 06:16
  • for backpress issue though its not a good solution you can maintain some `flag` for `onbackpressed` to decide whether to finish actvity or not when dialog show event is triggered – prashant17 Jul 23 '18 at 06:18