简体   繁体   English

从一个活动按下后退按钮可刷新堆栈中的上一个活动

[英]Pressing back button from one activity refreshes previous activity in stack

On pressing back button from child activity parent activity displays for a second and refreshes itself. 在从子活动按下后退按钮时,父活动将显示一秒钟并刷新自身。

In child activity I have this code in java file 在子活动中,我在java文件中有这个代码

@Override
public void onBackPressed()
{

    Intent moveback =
            new Intent(ClassActivityEdit.this, ClassActivity.class);
    startActivity(moveback);
    finish();
}

ClassActivityEdit is child class. ClassActivityEdit是子类。 In manifest file code is as follows 在清单文件代码中如下

<activity android:name=".ClassActivity"
        android:label="Class Activity">
        <intent-filter>
            <action android:name="com.teamtreehouse.oslist.ClassActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name=".ClassActivityEdit"
        android:label="Class Activity"
        android:noHistory="true">
        <intent-filter>
            <action android:name="com.teamtreehouse.oslist.ClassActivityEdit" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

On back button I just want the ClassActivity layout to be displayed without it being refreshed. 在后退按钮上我只想显示ClassActivity布局而不刷新它。

Edit: ClassActivityEdit doesnt extend ClassActivity . 编辑: ClassActivityEdit不会扩展ClassActivity Its just that some button press in ClassActivity will result in opening ClassActivityEdit . 只是在ClassActivity按下一些按钮将导致打开ClassActivityEdit

Edit2 EDIT2

the below code in ClassActivity starts ClassActivityEdit ClassActivity的以下代码启动ClassActivityEdit

public void editListener(View v) {
        Intent addNewClass =
                new Intent(ClassActivity.this, ClassActivityEdit.class);
        RelativeLayout buttonTableRow = (RelativeLayout) v.getParent();
        TextView getCourseID = (TextView) buttonTableRow.findViewById(R.id.courseNumberActivity);
        String courseIDString = getCourseID.getText().toString();
        Bundle bundle = new Bundle();

        //Add your data to bundle
        bundle.putString("CourseIDString", courseIDString);

        addNewClass.putExtras(bundle);
        startActivity(addNewClass);
}

Edit 3: I also have a Landing (MAIN) activity which flashes for a second. 编辑3:我还有一个着陆(MAIN)活动闪烁一秒钟。 On pressing back button from ClassActivityEdit activity Landing activity flashes again and then the ClassActivity activity loads. ClassActivityEdit活动中按下后退按钮时,登陆活动再次闪烁,然后加载ClassActivity活动。 Finding it a bit tricky to solve. 找到它有点棘手解决。

public class LoadingPage extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading_page);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent mainIntent = new Intent(LoadingPage.this, ClassActivity.class);
            startActivity(mainIntent);
        }
    }, 1000);
}

} }

The problem is with your override to the back press (which does not need to be overridden) and is likely a symptom of with what your activity does when focus returns to it. 问题在于你对后退的覆盖(不需要覆盖),并且很可能是当焦点返回时你的活动所做的一个症状。

Firstly if you don't override onBackPress the previous activity will load (that's the default behaviour because of the backstack ) so you don't need to manually call it with a new intent and tell manually tell it to go to the previous activity. 首先,如果你不覆盖onBackPress ,前一个活动将加载(这是因为backstack的默认行为)所以你不需要用新的意图手动调用它,并告诉它手动告诉它转到上一个活动。

When your parent activity (ClassActivity) then starts again it will go through the normal lifecycle - it will get resumed and all saved instance states get restored. 当您的父活动(ClassActivity)再次启动时,它将经历正常的生命周期 - 它将恢复并且所有保存的实例状态都将恢复。 Since you haven't posted it you need to make sure onResume and onRestart are not doing anything in your parent activity such as loading or setting data. 由于您尚未发布它,因此您需要确保onResumeonRestart在父活动中没有做任何事情 ,例如加载或设置数据。

If you do keep the onBackPress you wrote then it will create a new instance and onCreate will always be called, unless it is singleInstance flagged, in which case onNewIntent will be called, but neither of these things seem to be what you want. 如果你确实保留了你编写的onBackPress ,那么它将创建一个新实例,并且将始终调用onCreate ,除非它被标记为singleInstance ,在这种情况下将调用onNewIntent ,但这些东西似乎都不是你想要的。

In response to your Edit 3 you need to make sure that LoadingPage has android:noHistory="true" so that it is not available to the backstack and then finish it explicitly to clean it up when you start the main class 为了响应你的编辑3,你需要确保LoadingPage有android:noHistory="true"这样它就不能用于后台堆栈,然后在你启动主类时明确地完成它以清理它

That is a simple as calling finish when starting your intent 在开始你的意图时,这是一个简单的调用finish

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent mainIntent = new Intent(LoadingPage.this, ClassActivity.class);
        startActivity(mainIntent);
        finish(); //end the wrapping activity (LoadingPage)
    }
}, 1000);

The last thing you should be aware of is the difference between up and back navigation. 您应该注意的最后一件事是向上和向后导航之间的区别。 In case you are navigating back via the home/up button you should also tell the manifest that these two activities are related. 如果您通过home / up按钮导航回来,您还应该告诉清单这两个活动是相关的。 In the manifest entry for ClassActivityEdit you should add android:parentActivityName="com.teamtreehouse.oslist.ClassActivity" ClassActivityEdit的清单条目中,您应该添加android:parentActivityName="com.teamtreehouse.oslist.ClassActivity"

Just let the back button do what it normally does. 只需让后退按钮执行正常操作即可。 Don't override it and don't start a new activity. 不要覆盖它,也不要开始新的活动。 The parent activity is below the child activity in the stack, so it should appear when the child activity finishes. 父活动低于堆栈中的子活动,因此它应在子活动完成时显示。

By default, Android will retain activities in a stack, so when you go from Activity A to Activity B, Activity A will be brought back when you finish Activity B unless you do some other stuff like mess with the launchMode , finish A on returning from B, etc. 默认情况下,Android会将活动保留在堆栈中,因此当您从活动A转到活动B时,活动A将在您完成活动B时被带回, 除非您执行其他一些事情,例如使用launchMode ,完成A返回时B等

From what can be seen in your code, your problem should be solved by not overriding onBackPressed() in your child activity. 从您的代码中可以看到,您的问题应该通过不覆盖您的子活动中的onBackPressed()来解决。 Also, you should remove your <intent-filter> block for the child activity in the manifest. 此外,您应该删除清单中子活动的<intent-filter>块。

I recommend reading up on managing the activity lifecycle and tasks and back stack . 我建议阅读有关管理活动生命周期任务以及返回堆栈的信息

First of all there is no need of overriding onBackPressed(). 首先,不需要覆盖onBackPressed()。 As Doug mentioned the parent activity is below the child activity in the stack , so it would be visible just after the child activity finishes on back pressed. 正如Doug提到的那样,父活动低于堆栈中的子活动,所以在子活动完成后,它就会显示出来。

There is a suggestion for you as the ActionBarActivity is deprecated so it would be better to extend the class AppCompatActivity in your activity class. 有人建议您不要使用ActionBarActivity,因此最好在您的活动类中扩展AppCompatActivity类。

I have tried to replicate the issue but failed. 我试图复制这个问题但失败了。 So I have created a classes. 所以我创建了一个类。

LandingPage.java LandingPage.java

public class LandingPage extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loading_page);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(LandingPage.this, ClassActivity.class));
        }
    }, 1000);
}

}

ClassActivity.java ClassActivity.java

public class ClassActivity extends AppCompatActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class_activity);

    //Do whatever you want to  do

}
public void editListener(View v)
{
    Intent addNewClass = new Intent(ClassActivity.this,ClassActivityEdit.class);
    RelativeLayout buttonTableRow = (RelativeLayout) v.getParent();
    EditText getCourseID = (EditText) buttonTableRow.findViewById(R.id.courseNumberActivity);
    String courseIDString = getCourseID.getText().toString();
    addNewClass.putExtra("CourseIDString", courseIDString);
    startActivity(addNewClass);
}
}

ChildActivityEdit.java ChildActivityEdit.java

public class ClassActivityEdit extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class_activity_edit);
    String course_no = getIntent().getStringExtra("CourseIDString");
    Toast.makeText(ClassActivityEdit.this,course_no,Toast.LENGTH_SHORT).show();
    //Do whatever you want to  do

}
}

Your Problem is so much strange ! 你的问题太奇怪了! just by not overriding OnBackPressed you will get the result you want. 只是不重写OnBackPressed你会得到你想要的结果。 I guess the problem is in your manifest. 我猜问题就在你的清单中。

android:noHistory="true" 机器人:noHistory = “真”

It doesn't let the activity to stay in history stack, try removing it. 它不会让活动保留在历史堆栈中,请尝试将其删除。 also try this: 也尝试这个:

android:launchMode="singleInstance" 机器人:launchMode = “singleInstance”

By doing this, the activity wont become created again. 通过这样做,活动不会再次创建。

Your main problem is that you have overridden onBackPressed in the child activity. 您的主要问题是您在子活动中覆盖了onBackPressed。 What I understand from your code is- ClassActivity --> Parent Activity ClassActivityEdit --> Child Activity 我从您的代码中理解的是 - ClassActivity - > Parent Activity ClassActivityEdit - > Child Activity

In manifest, android:noHistory="true" is specified for child ie ClassActivityEdit activity. 在manifest中,为子类指定了android:noHistory =“true”,即ClassActivityEdit活动。 So it won't be stored in stack. 所以它不会存储在堆栈中。 Nothing is mentioned for parent. 父母没有提到任何内容。

In onBackPressed of your child activity you are again creating new Parent activity even though it is already present in the stack. 在您的子活动的onBackPressed中,您再次创建新的Parent活动,即使它已经存在于堆栈中。 It's just below your child activity. 它就在你孩子的活动之下。

Even if you don't override onBackPressed, parent activity will still load. 即使您不覆盖onBackPressed,父活动仍将加载。 In your case, it is creating many instances of parent activity. 在您的情况下,它创建了许多父活动实例。

One more thing, unless you want your activity to handle or respond to specific actions, you don't need to add intent filters for activity in manifest. 还有一件事,除非您希望您的活动处理或响应特定操作,否则您无需为清单中的活动添加意图过滤器。 According to documentation, 根据文件,

"Intent filter Specifies the types of intents that an activity, service, or broadcast receiver can respond to. An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component." “Intent过滤器指定活动,服务或广播接收者可以响应的意图类型。意图过滤器声明其父组件的功能 - 活动或服务可以做什么以及接收者可以处理什么类型的广播。打开组件以接收广告类型的意图,同时过滤那些对组件没有意义的组件。“

you can using Intent easily move for back Activity. 你可以使用Intent轻松移动返回Activity。 but your back activity refresh all date write in onresum() method then it esyly refresh data 但你的后台活动刷新所有日期写入onresum()方法,然后它仔细刷新数据

In child activity I have this code in java file 在子活动中,我在java文件中有这个代码

@Override
public void onBackPressed()
  {

   Intent moveback =
        new Intent(ClassActivityEdit.this, ClassActivity.class);
   startActivity(moveback);
   finish();
  }

When you start child activity from parent activity your parent activity move to stack and child activity display in onBackPressed event on child activity you create a new instance of parent activity and in activity life cycle onCreate event called first and views inside it creating by default values, so when you click on back button in child activity without creating a new instance of parent activity android display parent activity with previous savedInstanceState then onResume event called. 当您从父活动开始子活动时,您的父活动移动到堆栈,子活动显示在子活动的onBackPressed事件中,您创建父活动的新实例 ,并在活动生命周期onCreate事件中调用first,其中的视图按默认值创建,因此,当您单击子活动中的后退按钮而不创建父活动的新实例时,使用先前的savedInstanceState显示父活动,然后调用onResume事件。

for more details see Activity Life Cycle 有关详细信息,请参阅活动生命周期

Change your onBackPressed() in child activity to this or you can also remove the onBackPressed() from child activity child activityonBackPressed()更改为此,或者也可以从child activity删除onBackPressed()

@Override
public void onBackPressed(){
    super.onBackPressed();   // call super back pressed method  
}

Make sure that you don't finish() parent activity when you move from parent activity to child activity parent activity转移到child activity时,请确保没有完成() parent activity child activity

Try using FLAG_ACTIVITY_REORDER_TO_FRONT when starting the ClassActivity intent. 尝试使用FLAG_ACTIVITY_REORDER_TO_FRONT开始ClassActivity意图时。

you said, 你说,

On pressing back button from ClassActivityEdit activity Landing activity flashes again and then the ClassActivity activity loads. 在ClassActivityEdit活动中按下后退按钮时,登陆活动再次闪烁,然后加载ClassActivity活动。

you might be calling finish() somewhere in the ClassActivity which will cause the activity to be removed from the stack. 您可能在ClassActivity中的某处调用finish(),这将导致活动从堆栈中删除。 If so, remove it. 如果是这样,请将其删除。

@Override
public void onBackPressed(){
  finish();
  super.onBackPressed();
}

this will solve your issue. 这将解决您的问题。 finish() will destroy your child activity and super.onBackPressed() will redirect you to parent activity. finish()将破坏您的子活动,super.onBackPressed()会将您重定向到父活动。

You need to replace 你需要更换

startActivity(addNewClass);

with

startActivityForResult(addNewClass,101);

and then, in OnActivityResult you can refresh your data. 然后,在OnActivityResult中,您可以刷新数据。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==101){
       if(resultCode==RESULT_CANCELED){
           //refresh data accordingly
       }else if(resultCode==RESULT_OK){
           //refresh data accordingly
       }
    }
}

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

相关问题 后退按钮不会转到活动堆栈上的上一个活动 - Back button not going to previous activity on the activity stack 按下返回按钮不会显示上一个活动 - Previous Activity does not come on pressing back button 通过按返回按钮重新启动上一个活动 - Restarting the previous activity by pressing back button 按后退按钮没有回到以前的Android活动 - Pressing Back Button did not go back to previous activity Android go 如何通过按下带有一些数据的后退按钮返回上一个活动? - How to go back to the previous activity by pressing the back button with some data? 取消“异步任务”并按“返回”按钮返回到上一个活动 - Cancelling the Async Task and returning to previous activity on pressing Back Button 按下后退按钮时,导航抽屉将打开,而不是先前的活动 - While pressing back button navigation drawer is opening instead of previous activity 按电子邮件中的取消按钮后如何备份我们之前的活动? - How to back our previous Activity after pressing cancel button in email? 按返回按钮后,上一个活动(列表视图)为空 - After pressing back button, previous activity (listview) is emtpty 按下返回按钮后,从本地(android)活动对本地进行反应时,刷新到上一个场景的状态 - refresh state on going to previous scene in react native from native (android) activity after pressing back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM