Dispense o Android PopUpWindow ao mudar para uma nova guia no TabActivity?
-
26-09-2019 - |
Pergunta
Eu tenho uma MapActivity como uma das quatro guias em uma tabactividade. Essa MapActivity pode lançar um PopUpWindow que é uma lenda. O PopUpWindow permanece na tela, na parte superior do mapa, até que o botão "Mostrar legenda" seja clicado novamente (para frente e para trás, etc.).
O problema é que, quando um usuário muda para outra guia, o PopUpWindow permanece persistente com a visualização.
Eu tentei implementar o método onPause () na classe MapActivity e descartá -lo a partir daí. A força do aplicativo termina com esse método em vigor.
Qualquer ajuda? Obrigado!
EDIT: Aqui está alguns do meu código:
Na MainActivity, que estabelece as quatro guias:
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, FirstActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("game").setIndicator("First",
res.getDrawable(R.drawable.ic_tab_game))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("alerts").setIndicator("Second",
res.getDrawable(R.drawable.ic_tab_alert))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapActivity.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LastActivity.class);
spec = tabHost.newTabSpec("experience").setIndicator("Last",
res.getDrawable(R.drawable.ic_tab_experience))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
Agora, na minha classe MapActivity (que estende o MapActivity):
// Declare the Legend PopupWindow
mapLegendInflater = (LayoutInflater) MapActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mapLegendPopupLayout = mapLegendInflater.inflate(
R.layout.maptablegendpopuplayout, null, false);
mapLegendPopup = new PopupWindow(mapLegendPopupLayout,
(int) (0.45 * getApplicationContext().getResources()
.getDisplayMetrics().widthPixels),
(int) (0.33 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels), true);
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);
Boolean legendIsShown = false;
mapLegendButton = (Button) findViewById(R.id.buttonMapLegend);
mapLegendButton.setOnClickListener(mapLegendListener);
private OnClickListener mapLegendListener = new OnClickListener() {
public void onClick(View v) {
// Launch or dismiss the map legend popup
if (legendIsShown) {
mapLegendPopup.dismiss();
mapLegendButton.getBackground().clearColorFilter();
legendIsShown = false;
} else {
mapLegendPopup.showAtLocation(
findViewById(R.id.buttonMapLegend), Gravity.TOP
| Gravity.LEFT, 8,
(int) (0.23 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels));
mapLegendButton.getBackground().setColorFilter(
new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
// mapLegendButton.getBackground().setColorFilter(0xFFFFFF00,
// PorterDuff.Mode.MULTIPLY);
legendIsShown = true;
}
}
};
Espero que isso dê uma idéia de onde estou. Tudo funciona perfeitamente bem na guia Map. Somente quando você possui as guias da legenda e do comutador que ela ainda é exibida em outras visualizações.
Solução
Eu sei que você disse que a implementação do OnPause () não funcionou para você, mas eu tentei e implementando o OnResume () e OnPause () na MapActivity funciona para mim.
Eu precisava fazer uma visualização.post (new Runnable () {...}) em onResume (), pois não pude recriar o popupwindow durante o OnResume (), então tive que agendá -lo imediatamente depois:
package com.esri.android.tabdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class MapActivity extends Activity
{
private TextView textView = null;
private PopupWindow popupWindow = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Hello World from MapActivity");
setContentView(textView);
}
@Override
protected void onPause()
{
super.onPause();
if (popupWindow != null)
{
popupWindow.dismiss();
popupWindow = null;
}
}
@Override
protected void onResume()
{
super.onResume();
final Context context = this;
textView.post(
new Runnable()
{
public void run()
{
popupWindow = new PopupWindow(context);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(context);
button.setText("Hello");
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
}
});
linearLayout.addView(button);
popupWindow.setContentView(linearLayout);
popupWindow.showAtLocation(linearLayout, Gravity.LEFT | Gravity.BOTTOM, 10, 10);
popupWindow.update(256, 64);
}
}
);
}
}
Outras dicas
Você pode iniciar seu popupwindown como este:
mapLegendPopup = new PopupWindow(this);
mapLegendPopup.setContentView (itemizeView);
mapLegendPopup.setBackgroundDrawable (new BitmapDrawable()); // key is here
mapLegendPopup.setWidth ((int) (0.45 * getApplicationContext().getResources()
.getDisplayMetrics().widthPixels));
mapLegendPopup.setHeight((int) (0.33 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels));
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);
Você deve gerenciar seus diálogos usando o método OnCreatedialog (), conforme recomendado pela estrutura.
Dessa forma, sua caixa de diálogo se tornará parte de sua atividade e isso fará isso por si só.
Se você realmente não quer usar isso (não consigo ver nenhum motivo pelo qual seria esse o caso, mas ainda assim), você pode usar o setrownerActivity () em sua caixa de diálogo para atribuí -la à sua atividade.