当用户返回应用程序时,Android覆盖方法

当用户返回应用程序时,Android覆盖方法,android,Android,嗨,我正在尝试创建一个弹出窗口,当用户离开/返回应用程序时弹出。例如,一个电话打进来,他们想回到我希望弹出窗口显示的应用程序。我需要哪种覆盖方法来实现此功能?回答: 使用onResume()和/或onRestart()。这两种方法都将在应用程序隐藏后重新启动时运行(接听电话、关闭应用程序但不销毁应用程序等),但onResume()方法将在应用程序启动时运行,而onRestart()方法不会运行 // this method will run when the app is first start

嗨,我正在尝试创建一个弹出窗口,当用户离开/返回应用程序时弹出。例如,一个电话打进来,他们想回到我希望弹出窗口显示的应用程序。我需要哪种覆盖方法来实现此功能?

回答: 使用
onResume()
和/或
onRestart()
。这两种方法都将在应用程序隐藏后重新启动时运行(接听电话、关闭应用程序但不销毁应用程序等),但
onResume()
方法将在应用程序启动时运行,而
onRestart()
方法不会运行

// this method will run when the app is first started as well
public void onResume()
{
     super.onResume();
     // put your popup here 
}

public void onRestart()
{
     super.onRestart();
     // put your popup here
}

资料来源:


提示: 我建议在每个方法中显示一个,告诉您使用的是哪个方法,以便您可以测试每个方法何时执行

例如:

public void onResume()
{
     super.onResume();
     Toast.makeText(getApplicationContext(), "onResume()", 0).show();
}

public void onRestart()
{
     super.onRestart();
     Toast.makeText(getApplicationContext(), "onRestart()", 0).show();
}
答复: 使用
onResume()
和/或
onRestart()
。这两种方法都将在应用程序隐藏后重新启动时运行(接听电话、关闭应用程序但不销毁应用程序等),但
onResume()
方法将在应用程序启动时运行,而
onRestart()
方法不会运行

// this method will run when the app is first started as well
public void onResume()
{
     super.onResume();
     // put your popup here 
}

public void onRestart()
{
     super.onRestart();
     // put your popup here
}

资料来源:


提示: 我建议在每个方法中显示一个,告诉您使用的是哪个方法,以便您可以测试每个方法何时执行

例如:

public void onResume()
{
     super.onResume();
     Toast.makeText(getApplicationContext(), "onResume()", 0).show();
}

public void onRestart()
{
     super.onRestart();
     Toast.makeText(getApplicationContext(), "onRestart()", 0).show();
}
答复: 使用
onResume()
和/或
onRestart()
。这两种方法都将在应用程序隐藏后重新启动时运行(接听电话、关闭应用程序但不销毁应用程序等),但
onResume()
方法将在应用程序启动时运行,而
onRestart()
方法不会运行

// this method will run when the app is first started as well
public void onResume()
{
     super.onResume();
     // put your popup here 
}

public void onRestart()
{
     super.onRestart();
     // put your popup here
}

资料来源:


提示: 我建议在每个方法中显示一个,告诉您使用的是哪个方法,以便您可以测试每个方法何时执行

例如:

public void onResume()
{
     super.onResume();
     Toast.makeText(getApplicationContext(), "onResume()", 0).show();
}

public void onRestart()
{
     super.onRestart();
     Toast.makeText(getApplicationContext(), "onRestart()", 0).show();
}
答复: 使用
onResume()
和/或
onRestart()
。这两种方法都将在应用程序隐藏后重新启动时运行(接听电话、关闭应用程序但不销毁应用程序等),但
onResume()
方法将在应用程序启动时运行,而
onRestart()
方法不会运行

// this method will run when the app is first started as well
public void onResume()
{
     super.onResume();
     // put your popup here 
}

public void onRestart()
{
     super.onRestart();
     // put your popup here
}

资料来源:


提示: 我建议在每个方法中显示一个,告诉您使用的是哪个方法,以便您可以测试每个方法何时执行

例如:

public void onResume()
{
     super.onResume();
     Toast.makeText(getApplicationContext(), "onResume()", 0).show();
}

public void onRestart()
{
     super.onRestart();
     Toast.makeText(getApplicationContext(), "onRestart()", 0).show();
}

根据您的需要,查看or的生命周期


您可能需要查看或的生命周期,具体取决于您的需要


您可能需要查看或的生命周期,具体取决于您的需要


您可能需要查看或的生命周期,具体取决于您的需要


您可能需要
onResume()
onRestart()
尝试,并朝
广播接收器的方向看
尝试,朝
广播接收器的方向看
尝试,并朝
广播接收器的方向看
尝试,看看
BroadcastReceivers

用户为什么会想要这个?@commonware这不是一个广告,这是一个用户安全的pin码输入表单本质上,应用程序包含用户的个人数据,用户在其中设置了一个pin码。我想添加这个pin码,以便用户必须输入pin码返回应用程序时访问该页面。就用户安全而言,问题不在于用户何时返回应用程序,而在于用户在X个时间段内未使用应用程序,因此其身份验证凭据已过期。仅仅因为用户离开你的应用程序几秒钟,就用身份验证对话框纠缠用户,这会使你非常不受欢迎。@commonware不一定。我已经下载了应用程序Vault,它实现了他所说的他想做的事情。我不觉得这很烦人。用户为什么要这样做?@Commonware这不是广告,这是用户安全的pin码输入表单本质上应用程序包含用户的个人数据,用户在其中设置了一个pin码。我想添加这个,以便用户在返回应用程序时必须输入pin才能访问页面。就用户安全而言,问题不在于用户何时返回应用程序,而在于用户在X个时间段内未使用应用程序,因此其身份验证凭据已过期。仅仅因为用户离开你的应用程序几秒钟,就用身份验证对话框纠缠用户,这会使你非常不受欢迎。@commonware不一定。我已经下载了应用程序Vault,它实现了他所说的他想做的事情。我不觉得这很烦人。用户为什么要这样做?@Commonware这不是广告,这是用户安全的pin码输入表单本质上应用程序包含用户的个人数据,用户在其中设置了一个pin码。我想添加这个,以便用户在返回应用程序时必须输入pin才能访问页面。就用户安全而言,问题不在于用户何时返回应用程序,而在于用户在X个时间段内未使用应用程序,因此其身份验证凭据已过期。仅仅因为用户离开你的应用程序几秒钟,就用身份验证对话框纠缠用户,这会使你非常不受欢迎。@commonware不一定。我已经下载了应用程序Vault,它实现了他所说的他想做的事情。我不觉得这很烦人。用户为什么要这样做?@commonware这不是广告,这是用户安全的pin码输入表单本质上该应用包含用户的个人数据,用户在开始时在其中设置pin码