我想完成()一个在透明 Activity 下的暂停 Activity 。
我有一个 Activity ,称为 Activity A。 Activity A 处于 Activity 状态时会发生两件事;
我们可以启动(透明的)Activity B
我们可以接收到一个异步回调来完成Activity A。
这两个 Action 发生的非常接近。代码是这样的
public class ActivityA extends Activity
{
public class DataHandler implements ContentLoader.OnDataListener
{
@Override
public void onData(Cursor data)
{
_binder.bind(data);
}
}
//If this callback is executed while Activity A is paused, it will not go into onStop until it the activity above it is finished
private class LoaderCallbacks extends ContentLoader.LoaderCallbacks
{
public LoaderCallbacks(ContentLoader loader)
{
super(loader);
}
@Override
public void onLoadFinished(
Loader<Cursor> loader,
Cursor cursor)
{
if (cursor == null || cursor.getCount() <= 0)
{
Log.d("Eric", "* ON FINISH *");
finish();
finishagain();
return;
}
super.onLoadFinished(loader, cursor);
}
}
}
在这个 Activity 显示的列表 fragment 内部有一个启动 Activity B的机制
public class FragmentA extends ListFragment
{
//Some fragment functions here...
@Override
public void onListItemClick(
ListView list,
View view,
int position,
long id)
{
Intent intent = new Intent();
intent.setAction(Intent.LAUNCH_ACTIVITY_B);
getActivity().sendBroadcast(intent)
}
}
我的问题是在 Activity B 启动后调用完成 Activity A 的回调时, Activity A 不会立即完成。它一直处于暂停状态,直到 Activity B 完成,然后两者都完成。这是一个竞争条件,我已经通过在暂停状态下使用简单的等待线程再次尝试完成来确认这一点。正如预期的那样,所有的完成调用都在主线程上执行。
private void finishagain()
{
Handler handler = new Handler();
int LOCK_HOME_DELAY = 5000;
handler.postDelayed(new Runnable()
{
public void run()
{
if (notfinished){
Log.d("Eric", "*************** FINISH AGAIN ****************");
finish(); //Does nothing while the activity is paused
}
else{
Log.d("Eric", "* Times up do nothing *");
}
}
}, LOCK_HOME_DELAY);
}
这是我的日志(一些包名可能会被编辑)
10-10 18:23:05.168 74-98/system_process I/ActivityManager: Displayed somepackage/com.eric.activity.A: +894ms
10-10 18:23:07.135 74-98/system_process I/ActivityManager: Displayed somepackage/com.eric.activity.B: +343ms
10-10 18:23:07.102 547-547/somepackage D/Eric: * Times up do nothign *
10-10 18:23:07.231 547-547/somepackage D/Eric: * ON FINISH *
10-10 18:23:08.220 547-547/com.eric.Status D/Eric: * Times up do nothign *
10-10 18:23:08.305 547-547/com.eric.Status D/Eric: * Times up do nothign *
10-10 18:23:12.305 547-547/com.eric.Status D/Eric: *************** FINISH AGAIN ****************
10-10 18:23:12.305 74-668/system_process W/ActivityManager: Finishing task with all activities already finished
10-10 18:23:12.305 74-668/system_process W/ActivityManager: Duplicate finish request for ActivityRecord{3627639c u0 somepackage/com.eric.activity.A t2292 f}
(注意时间戳 - 我在 :07 秒调用完成,但它没有完成。finishAgain() 在 :12 秒再次调用完成,它似乎在这里关闭,但我也看到它稍后完成。另外请注意“重复的完成请求” - 对我来说,完成看起来像是排队或其他东西)。
如何让 Activity A 在透明 Activity B 下方暂停时完成?
说实话,我很惊讶这是一个问题;我认为 backstack 上的 Activity 应该很容易被杀死,但可能不是那些处于 onPause 状态的 Activity ?我一直无法找到这方面的文档,也许有人知道相关的文档/代码?
编辑看我的回答
最佳答案
嗯,这个问题比看起来更棘手,透明的 Activity 会导致问题。你解释得很好,但是人们通常不会回答这个问题,他们会泄露一些他们熟悉的东西。
"If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations." see android doc.
你说:
//If this callback is executed while Activity A is paused, it will not go into onStop until it the activity above it is finished
当 Activity A 可见时,您不能进入停止状态。 Android destroying activities, killing processes
这可能会有所帮助:
View topLevelLayout = findViewById(R.id.top_layout);
topLevelLayout.setVisibility(View.INVISIBLE);
在我的回答中,默认情况是异步的(它是广播)。
在创建时为 本地广播 注册每个 Activity(您想稍后杀死,ActivityA)。
当它进入后台时(暂停状态),
(即,一个新的 Activity 到达堆栈的顶部),
它的 onPause() {onStop()} 将被调用,但它仍然可以接收广播。
您只需要确保在 onDestroy() 而不是在 onPause() {onStop()} 中调用 unregisterReceiver()。
这里有一些示例代码可以让您了解一下:
/** your "kill" method (ActivityA) **/
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_KILL");//some string
// sendBroadcast(broadcastIntent);//global broadcast
LocalBroadcastManager.getInstance(this).sendBroadcast(broadcastIntent);
/** The receiver (ActivityA) **/
BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_KILL");//some string
LocalBroadcastManager.getInstance(this).registerReceiver(myBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive","KILL in progress");
//At this point you should do your stuff ;O)
finish();//kill it
//System.exit(0)// to clear static variables
//android.os.Process.killProcess(android.os.Process.myPid());//"cave man" kill }
}, intentFilter);
}
protected void onDestroy(
{
LocalBroadcastManager.getInstance(this).unregisterReceiver(myBroadcastReceiver );
super.onDestroy();
}
https://developer.android.com/training/basics/activity-lifecycle/index.html https://developer.android.com/reference/android/app/Activity.html https://developer.android.com/reference/android/content/BroadcastReceiver.html
关于android - 在重新获得焦点之前无法完成暂停的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964817/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类