返回上一个活动(PreferenceManager)(Return to the last activity (PreferenceManager))
我想返回上一个活动,用户在关闭应用程序之前已关闭。 我已经实现了这个方法来传达3个活动,但在我的代码中有一些错误。 你有什么想法解决这个问题吗?
MenuActivity(启动应用时打开)
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String lastActivity = ((Intent) PreferenceManager.getDefaultSharedPreferences(this)).getStringExtra("sent"); if (lastActivity != null && lastActivity.equals("activity_2")){ { Intent myIntentActivity1 = new Intent(menu.this,activity_2.class); startActivity(myIntentActivity1); } } if (lastActivity != null && lastActivity.equals("activity_3")){ { Intent myIntentActivity2 = new Intent(menu.this,activity_3.class); startActivity(myIntentActivity2); } } else { Intent myIntentActivity3 = new Intent(menu.this,menu.class); startActivity(myIntentActivity3); }
活动2
//一些代码
@Override public void onResume(){ super.onResume(); Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit(); ((Intent) e).putExtra("sent", "activity_2"); e.commit(); }
活动3
//一些代码
@Override public void onResume(){ super.onResume(); Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit(); ((Intent) e).putExtra("sent", "activity_3"); e.commit(); }
I would like to return to the last activity, the user has closed before closing down the application. I have implemented this method to communicate the 3 activities, but in my code there is something wrong. Do you have any idea to solve this problem?
MenuActivity (opens when the app is launched)
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String lastActivity = ((Intent) PreferenceManager.getDefaultSharedPreferences(this)).getStringExtra("sent"); if (lastActivity != null && lastActivity.equals("activity_2")){ { Intent myIntentActivity1 = new Intent(menu.this,activity_2.class); startActivity(myIntentActivity1); } } if (lastActivity != null && lastActivity.equals("activity_3")){ { Intent myIntentActivity2 = new Intent(menu.this,activity_3.class); startActivity(myIntentActivity2); } } else { Intent myIntentActivity3 = new Intent(menu.this,menu.class); startActivity(myIntentActivity3); }
Activity 2
//some code
@Override public void onResume(){ super.onResume(); Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit(); ((Intent) e).putExtra("sent", "activity_2"); e.commit(); }
Activity 3
//some code
@Override public void onResume(){ super.onResume(); Editor e = PreferenceManager.getDefaultSharedPreferences(this).edit(); ((Intent) e).putExtra("sent", "activity_3"); e.commit(); }
原文:https://stackoverflow.com/questions/20109003
最满意答案
在提交给
ExecutorService
的任务中,接收中断是取消执行任务的信号。 因此,在您的代码示例中,答案是“否”,请勿再次设置中断。就源代码中所见,重新声明中断状态将被忽略,但它确实在执行程序中浪费了一些工作,因为如果工作线程尝试获取另一个任务,则会立即引发
InterruptedException
,然后根据执行者的状态确定是虚假的并清除。及时关闭执行程序取决于响应中断而退出的任务; 它不依赖于恢复中断状态的任务。
In a task submitted to an
ExecutorService
, receiving an interrupt is a signal to cancel execution of the task. So, in your code example, the answer is "no", don't set the interrupt again.Re-asserting the interrupt status, as far as I can see in the source code, will be ignored, but it does waste a bit of work in the executor as an
InterruptedException
is raised immediately if the worker thread tries to get another task, which is then determined to be spurious and cleared based on the state of the executor.Shutting down the executor in a timely manner depends on tasks exiting in response to an interrupt; it does not depend on tasks restoring the interrupt status.
相关问答
更多-
ExecutorService抽象出许多与较低级别抽象(如原始Thread相关的复杂性。 它为成功或突然终止任务(表示为Runnable或Callable )提供了安全启动,关闭,提交,执行和阻止的机制。 从JCiP ,第6.2节,直接从马的嘴巴: Executor可能是一个简单的接口,但它构成了异步任务执行的灵活和强大框架的基础,该框架支持各种任务执行策略。 它提供了将 任务提交与 任务执行分离的标准方法,将任务描述为 Runnable 。 Executor实现还提供了生命周期支持和钩子,用于添加统计信息 ...
-
可以有许多这些ParallelUtils同时运行(从不同的线程启动) 显然,这些线程中的至少一个直接通过Thread.interrupt()或间接通过例如Future.cancel(true)或ExecutorService.shutdownNow()中断。 重现此中断的示例: class Sleep implements ParallelUtil.Task
{ @Override public void execute(Integer data) throws Exception ... -
Java ExecutorService - 任务/可调用不取消/中断(Java ExecutorService - Task/Callable not cancelling/interrupting)[2022-11-01]
根据ExecutorService文档,关闭正在执行的任务是尽最大努力完成的。 因此,当您调用ExecutorService.shutdownNow() ,实现将尝试关闭所有当前正在执行的任务。 每个任务将保持运行,直到它检测到它被中断为止。 为了确保你的线程在早期阶段达到了这一点,最好在你的循环中添加一个检查线程是否被中断,如下所示: Thread.currentThread().isInterrupted(); 通过在每次迭代时进行此调用,您的线程将从实际中断中以很短的时间间隔检测到中断。 所以你修改 ... -
代替 count.set(count.get() + i); 使用 count.addAndGet(i); 方法addAndGet以原子方式添加值,但顺序get和set不是原子操作。 Instead of count.set(count.get() + i); use count.addAndGet(i); Method addAndGet adds value atomically but sequential get and set is not atomic operation.
-
在提交给ExecutorService的任务中,接收中断是取消执行任务的信号。 因此,在您的代码示例中,答案是“否”,请勿再次设置中断。 就源代码中所见,重新声明中断状态将被忽略,但它确实在执行程序中浪费了一些工作,因为如果工作线程尝试获取另一个任务,则会立即引发InterruptedException ,然后根据执行者的状态确定是虚假的并清除。 及时关闭执行程序取决于响应中断而退出的任务; 它不依赖于恢复中断状态的任务。 In a task submitted to an ExecutorService, ...
-
Java中有两种线程(当然取决于你如何看待它们)。 '用户'线程和'守护进程'线程。 您的应用程序在以下某种情况下结束: 你调用System.exit() 您的应用程序中没有User线程。 这在这里解释。 请注意,您的main功能由JVM在“用户”线程上执行,这意味着只要您尚未完成main功能。 大多数多线程应用程序只运行main函数来启动所需的所有线程。 守护程序线程背后的想法是您可以(定期)执行某些操作,但如果完成所有其他任务,则不会阻止应用程序退出。 默认情况下,新线程是“非守护程序”线程,对于Exe ...
-
不需要特定于JavaFX的执行器服务:常规的java.util.concurrent.ExecutorService工作得很好,因为Task是FutureTask的子类。 获得任务列表后,您可以根据每个任务的进度计算总体进度。 例如,它可能只是所有进度的总和除以任务数。 如果每个任务都有不同数量的项目要处理,您可能会做一些更复杂的事情。 这是一个简单的SSCCE: import java.util.ArrayList; import java.util.List; import java.util.Rand ...
-
ExecutorService永远不会停止。(ExecutorService never stops. When execute new task inside another executing task)[2022-05-06]
我从前面看到你的评论: 我不能使用CountDownLatch,因为我事先不知道我将从资源中收集多少个唯一链接。 首先,vsminkov就是为什么awaitTermniation将等待10分钟的答案。 我将提供替代解决方案。 而不是使用CountDownLatch使用Phaser 。 对于每个新任务,您都可以注册并等待完成。 创建单个移相器并在每次调用execute.submit时进行register并在每次Runnable完成时arrive 。 public void crawlInternetResou ... -
最后,您正在关闭线程池中的所有线程。 但还有另一个非守护进程线程阻止JVM终止。 你能发现它吗? 这是你的匿名生产者线程,里面有无限循环: for (;;) 。 使用Thread.setDaemon(true) : Thread t = new Thread(new Runnable() { //... }); t.setDaemon(true); t.start(); 现在当ExecutorService所有线程在关闭后终止时, main线程也会终止,JVM将停止,因为你唯一剩下的线程是一个守护进程 ...
-
看起来你可以简单地维护一个Map
, Callable >而不是List >并以这种方式检索原始的Callable。 如果你想变得非常聪明,你可以做OO风格并扩展ThreadPoolExecutor并创建一个Future装饰器类。 我认为这可能是矫枉过正,但你可以这样做: import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException ...