简体   繁体   English

当我完成结果活动时调用 OnCreate 和 onActivityResult

[英]OnCreate and onActivityResult called when I finish the activity for result

I have Activity A. it displays some data retrieved from the server by calling void loadData() in onCreate.我有活动 A。它通过在 onCreate 中调用 void loadData() 来显示从服务器检索的一些数据。

User click a button which open a new Activity B for result.用户单击一个按钮,打开一个新的活动 B 以获取结果。 Once it finishes it back to Activty A. onActivtyresult should call loadData() to reload the data.完成后返回 Activty A。 onActivtyresult调用 loadData()以重新加载数据。

When Activty B finishes both onCreate and onActivityResult called as A was destroyed.当 Activity B 完成 onCreate 和 onActivityResult 调用时,A 被销毁。 Which is not Ok for me.这对我来说不好。

The question is:问题是:

  • Dose activity A always will be destroyed and onCreate will be called?剂量活动 A 总是会被销毁并且 onCreate 会被调用? Is this documented as I could not find any thing about this.这是否记录在案,因为我找不到任何关于此的信息。 If this is the case then I will call loadData only in onCreate.如果是这种情况,那么我将只在 onCreate 中调用 loadData。

    If not I have to call loadData from onActivityResult also, which again leaded to call loadData 2 time in some cases, which I dont want.如果不是,我也必须从 onActivityResult 调用 loadData ,这在某些情况下再次导致调用 loadData 2 次,这是我不想要的。

    Class A: A类:

     public void onCreate(@Nullable Bundle bundle) { .... creation code loadData(); // will be called first time activity created as well when back from the activity B } public void createNew() { Intent createService = new Intent(this,CreateService.class); startActivityForResult(createService, 1001); overridePendingTransition(R.anim.out_top,R.anim.fp); } public void onActivityResult(int i, int i2, Intent intent) { super.onActivityResult(i, i2, intent); if (i2 != -1) { loadData(); ///will be called when back from Activty B } }

    Class B: B类:

     public void on taskFinish() { setResult(1); finish(); overridePendingTransition(R.anim.s_right, R.anim.s_in_right); }

I have come with a work around like this我带着这样的工作来了

     boolean loaded = false;
     void onCreate() {
        loadData();
        loaded=true;
     }

    public void createNew() {
        loaded = false;  /// Set before start the new Activity
        Intent createService =  new Intent(this,CreateService.class);
        startActivityForResult(createService, 1001);
        overridePendingTransition(R.anim.out_top,R.anim.fp);
     } 

     public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 != -1) {
           if (!loaded) {
             loadData();
             loaded = true;
          }
       }
     }

I have 2 scansion我有 2 个扫描

1- Activity created form scrach- loadData will be called once in OnCrate() 1- Activity 创建的表单 scrach-loadData 将在 OnCrate() 中调用一次

2- Start new Activity for result - set loaded = false 2- 为结果启动新活动 - 设置加载 = false

a- after back for result if activity destroyed then loaded == true in onCreate and loadData will be called once. a- 在返回结果后,如果活动被破坏,则在 onCreate 中加载 == true 并且 loadData 将被调用一次。 then onActivityResult will be called but loadData will not be called.然后将调用 onActivityResult 但不会调用 loadData。

b- after back for result if Activity not destroyed then loaded = false and loadData will be called b-返回结果后,如果 Activity 未销毁,则加载 = false 并且将调用 loadData

No, there is no guarantee that Activity A will be destroyed.不,不能保证 Activity A 会被销毁。 You'd need to find some way of tracking whether the data needs to be reloaded - perhaps you could pass back a timestamp or something similar in the result from Activity B that you could use to check if the data you have in Activity A is up-to-date.您需要找到某种方法来跟踪是否需要重新加载数据 - 也许您可以在活动 B 的结果中传回时间戳或类似的东西,您可以用它来检查活动 A 中的数据是否已启动-迄今为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将完成选择的图像时未调用onActivityResult - onActivityResult not called when I will finish selected image 在`finish()`之后没有调用onActivityResult - 调用被调用的活动 - onActivityResult not being called after `finish()`-ing the called activity 当我切换到活动或首先调用 onDataChanged 时,onCreate() 未运行 - onCreate() is not running when i switch to the activity or onDataChanged is getting called first 当我启动 Activity 时,在 onCreate() 之后调用 OnDestroy() - OnDestroy() is being called after onCreate() when I start Activity 当我返回到第一个活动时,第二个活动未调用第二个活动的onCreate()函数 - When I return back to 1st Activity the onCreate() function of 2nd Activity is not called in 2nd time 在finish()之后不会调用onCreate吗? - onCreate not getting called after finish()? 设置活动onCreate()未调用 - Settings Activity onCreate() not called 启动另一个 Activity 时再次调用 Launch-Activity onCreate() - Launch-Activity onCreate() called again when start another Activity Activity onCreate之后调用(未调用)应用程序onCreate - Application onCreate called(not called) after Activity onCreate 将数据从片段发送到另一个活动时未调用OnActivityResult - OnActivityResult is not called when sending data from fragment to another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM