Android:重新加载主要活动

时间:2013-12-18 11:59:18

标签: android android-activity alertdialog

上行链接与我的问题不同

我有这个结构: 主要 - > MenuItem - > AlertDialog类 - >点击按钮 - > (重新加载)主要

我想点击按钮重新加载活动主要。我怎么能成功呢?

由于

* CLASE MAIN *

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onPrepareOptionsMenu(Menu menu) {

    MenuItem item = menu.findItem(R.id.MenuOpcColores);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.MenuOpcColores:
            CambiarColores colores = new CambiarColores(this, 
                    R.layout.cambiar_colores, 
                    R.string.cambiarColores, 
                    R.color.estandar, 
                    R.id.botonAplicarColor,
                    R.id.botonCancelarColor); 
            colores.show();  
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

CLASE DIALOG

public class CambiarColores extends Dialog implements          android.view.View.OnClickListener{  

Context context;


public CambiarColores(Context context, int vista, int titulo, int color, int id1, int id2) {  

    super(context);  
    this.context = context;
    this.titulo = titulo;
    this.vista = vista;
    this.color = color;
    this.id1 = id1;
    this.id2 = id2;
}  

protected void onCreate(Bundle savedInstanceState) {  

    super.onCreate(savedInstanceState);

    setContentView(vista);  
}  

@Override  
public void onClick(View v) {  

    // REFRESH MAIN 

} 
} 

2 个答案:

答案 0 :(得分:2)

重新加载活动,先完成或完成活动,然后调用意图。

finish();
startActivity(getIntent());

或关注此帖子, https://stackoverflow.com/a/6283098/6780216

答案 1 :(得分:1)

Button点击执行以下代码:

Intent intent = new Intent(context,MainActivity.class);
context.startActivity(intent);