首页 \ 问答 \ 返回上一个活动(PreferenceManager)(Return to the last activity (PreferenceManager))

返回上一个活动(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
更新时间:2021-11-12 15:11

最满意答案

在提交给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 ...
  • 根据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 ...
  • 我从前面看到你的评论: 我不能使用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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)