简体   繁体   English

如何从活动第3过渡到第1?

[英]How to transition from activity 3rd to 1st?

I have a problem with Shared Element Activity Transition between activities. 我在Shared Element Activity Transition之间Shared Element Activity Transition遇到问题。 I have MainActivity, it has a recyclerview with boxes (recyclerview.horizontal). 我有MainActivity,它有一个带有框的recyclerview (recyclerview.horizo​​ntal)。 Each box when clicked will go to the corresponding activity. 单击每个框将转到相应的活动。 The problem that appears when I click on a box, I switch to the second activity, in the second activity I press a button to switch to the third activity. 当我单击一个框时,出现问题,我切换到第二个活动,在第二个活动中,我按下按钮以切换到第三个活动。 And here I swipe to right to return to the MainActivity with transition and I want it to transition right to the box corresponding to the 3rd activity in the recyclerview in MainActivity. 在这里,我向右滑动以返回带有过渡的MainActivity,我希望它向右过渡到与MainActivity中recyclerview中的第3个活动相对应的框。 So, my purpose is: 因此,我的目的是:

MainActivity (Shared Element Activity Transition)-> Second Activity -> Third Activity (Shared Element Activity Transition)-> MainActivity (exactly scroll to position for Third Activity in RecyclerView). MainActivity(共享元素活动转换)->第二活动->第三活动(共享元素活动转换)-> MainActivity(精确滚动到RecyclerView中第三活动的位置)。

My MainActivity I hope everyone offer me the solution. 我的MainActivity希望每个人都能为我提供解决方案。 Thank you so much. 非常感谢。

You can use startActivityForResult instead of startActivity in SecondActivity when you are going to start ThirdActivity.Like this : 当您要启动ThirdActivity时,可以在SecondActivity中使用startActivityForResult而不是startActivity。

Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);

And when you are finishing your ThirdActivity 当您完成ThirdActivity时

Intent returnIntent = new Intent();
returnIntent.putExtra("activity_finish",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();

If you use startActivityForResult() then it gives callback in the Activity who started it,so as soon as ThirdActivity finishes, it will return to the onActvityResult() in SecondActivity.Where you have to check result_code and request code like this : 如果您使用startActivityForResult(),它将在启动它的Activity中提供回调,因此ThirdActivity一旦完成,它将返回SecondActivity中的onActvityResult()。在这里,您必须检查result_code并请求这样的代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        boolean isActivityFinish=data.getBooleanExtra("activity_finish");
         if(isActivityFinish){
          // finish your Second Activity here
          }


    if (resultCode == Activity.RESULT_CANCELED) {
        //Write your code if there's no result
    }
}

} }

For more info : How to manage `startActivityForResult` on Android? 有关更多信息: 如何在Android上管理`startActivityForResult`?

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

相关问题 如何将价值从第一个活动传递到第三个活动 - How to pass value from 1st activity to 3rd activity 从第三项活动转到第一项活动 - Going from 3rd Activity to 1st Activity 将清单从第一个活动传递到第三个活动 - Passing List from 1st activity to 3rd activity 从第一活动到第三活动的变量值传递 - Passing value of a variable from 1st activty to 3rd activity 如何将文本视图数据从第一活动发送到第三活动 - How to send Text View data from 1st activity to 3rd activity 从第3个Activity调用setResult时,将数据返回第一个Activity - Returning data back to 1st Activity when setResult is called from 3rd Activity 从3rdfragment选项卡过渡到2nd / 1stfragment选项卡,中断了应用程序 - Transition from 3rd fragment tab to 2nd/1st fragment tab , breaks the app 如何获得有关按钮单击第一活动的信息并在第三活动中使用它的信息? -安卓 - How to get information about a button click on 1st activity and use it in the 3rd activity? - android 在 android 中将数据从第一个选项卡传递到第三个选项卡活动时获取 null object 引用 - Getting null object reference when passing data from 1st tab to 3rd tab activity in android 从后台堆栈中的第 3 个片段到第 1 个片段:Android - Going from 3rd to 1st fragment in the backstack: Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM