Java 如何在静态方法中刷新相同的活动
我的应用程序需要关闭“活动”并使用“刷新活动”直接打开它,还是打开“新活动”并返回“活动”Java 如何在静态方法中刷新相同的活动,java,android,Java,Android,我的应用程序需要关闭“活动”并使用“刷新活动”直接打开它,还是打开“新活动”并返回“活动” public class MainActivity extends Activity { static TextView messageBox; static String x=""; static Context context; @Override protected void onCreate(Bundle savedInstanceState) {
public class MainActivity extends Activity {
static TextView messageBox;
static String x="";
static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageBox=(TextView)findViewById(R.id.messageBox);
check();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static void updateMessageBox(String msg,String from)
{
messageBox.append(msg);
//Get parent phone number from database
//check parent number with ...
if(msg.equals("enable wifi"))
{
x="yes";
}
//context.startActivity(new Intent(context, MainActivity.class));
Intent i=new Intent(context, p1.class);
context.startActivity(i);
}
private void check()
{
if(x.equals("yes"))
{
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
}
}
}
我正在使用“打开新活动”并返回活动,但应用程序的工作崩溃
请帮助我???您只需使用:
finish();
startActivity(getIntent());
要从活动本身刷新活动,请尝试以下操作:
Intent intent = getIntent();
finish();
startActivity(intent);
或
如果应用程序的最小API>11,则可以调用Activity.recreate()代码>
如果您在API11+环境中,这是最好的。如果您使用的是API 10或更低版本,您仍然可以检查当前版本并调用上面的代码段。(请别忘了投票支持拉尔夫的答案!)这和我给出的答案是一样的:)如果日志崩溃,请发布日志不要在emulator上运行,只能在phone上运行是的,但是当你从eclipse/intellij/android studio在手机上运行时,您有一个显示错误/警告等的日志。您如何在设备上测试您的应用程序?直接运行应用程序,在两个分区之间发送sms