1

我是 Android 应用程序开发的新手。当单击项目时触发操作的 ListView 时,我尝试创建一个新活动。

在测试期间,我的代码正在运行。但是我对是否应该使用 finish() 关闭当前活动有点困惑。哪一个更好?从性能或资源使用的角度来看?

下面是我的一些代码:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bank_name);
    lv1.setAdapter(adapter);
    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
                public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)   {

                       // TODO Auto-generated method stub
                      if (position == 0) {
                          return_to_config(view);
                      }


                }
    });


public void return_to_config(View view){
       Intent intent = new Intent(this, ConfigActivity.class);
       startActivity(intent);
       *this.finish();*
}
4

2 回答 2

1

I don't think you need to worry about resource or performance in this situation. You should be most concerned on the functionality that you want and your users expect.

If they are clicking a list item and open up say a detailed Activity for that list item then they press the back button then they probably expect to return to that list. If you call finish() then the user will return to the Activity before this one or if there are no others then they will exit the app. Is that what you want?

So with what little I know about your app I would say remove finish()

public void return_to_config(View view){
   Intent intent = new Intent(this, ConfigActivity.class);
   startActivity(intent);

}

But if the user doesn't expect to return here when pressing "Back" button then keep it in there

于 2013-07-30T14:50:57.743 回答
0

您可以覆盖 onPause() 并在那里编写 finish() 而不是这样做。效率更高

于 2013-07-30T14:52:16.720 回答