sean
sean

Reputation: 51

open a new activity in original activity and finish original one

I am new to Android application development. I tried to create a new activity when a ListView which triggers an action when an item is clicked.

During testing, my code was working. But I am a little bit confused on whether I should use finish() to close the current activity. Which one is better? From performance or resource usage point of view?

Below is some of my code :

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();*
}

Upvotes: 1

Views: 77

Answers (2)

Ashwin Balani
Ashwin Balani

Reputation: 771

Instead of doing this, you can override the onPause() and write finish() over there. It is more efficient

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

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

Upvotes: 1

Related Questions