Reputation: 607
My app runs continuously and because of this I get a lot of popup windows open at the same time. I have tried to put following code:
if (pwindo.isShowing())
{
pwindo.dismiss();
}
in places of my code marked by **********************************:
public class travel extends AppCompatActivity {
private PopupWindow pwindo;
Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.travel_layout2);
mHandler.postDelayed(runnable, 100);
}
private Runnable runnable = new Runnable()
{
public void run()
{
for (int i=begin;i<=end;i++)
{
if ((gps > Float.parseFloat(words[2+i*6])-0.11)&& (counter==i)&&(gps < Float.parseFloat(words[2+i*6])))
{
if ((words[3+i*6]!= null && !words[3+i*6].isEmpty())||(number==end)||(count>0)) {
initiatePopupWindow();
}
}
}
mHandler.postDelayed(runnable, 100);
if (gps > latEnd)
{
mHandler.removeCallbacks(runnable);
}
}
};
//****************************************************
private void initiatePopupWindow()
{
//****************************************************
try
{
//****************************************************
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_popup,(ViewGroup) findViewById(R.id.popup_element));
pwindo = new PopupWindow(layout, 700, LayoutParams.FILL_PARENT, true);
//****************************************************
pwindo.setBackgroundDrawable(new BitmapDrawable());
//****************************************************
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
but it didn't work in any case. Can anyone tell me what am I doing wrong? (inOncreate function I am calling runnable function, which runs every couple seconds and if some conditions are fulfilled initiatePopupWindow() function is called in order to open popup window)
Upvotes: 1
Views: 2858
Reputation: 105
You can set flag for it. Like;
if(flag)
{
initiatePopupWindow();
flag==false;
}
Upvotes: 0
Reputation: 2070
before calling initiatePopupWindow() check if pwindo
is already there.
It is likely that you are getting popups layering ontop of themselves and the pwindo
variable is only storing the address of the most recent one.
Upvotes: 1