简体   繁体   中英

Finish or destroy of activity on clicking of Listview button after starting of Another activity

I have an Activity ListActivity having Listview and another class CustomListAdapter extends BaseAdapter.

Code in ListActivity

customAdapter = new CustomListAdapter(list);
TripList.setAdapter(customAdapter);

In getView() of CustomListAdapter I inflate a layout. there is a button and on click of that button I am starting another activity.

I want to finish ListActivity after start of another activity. With the below code My app is crashing.

((Activity) ctx).finish();

Log is

01-05 11:06:04.319: E/AndroidRuntime(4319): FATAL EXCEPTION: main
01-05 11:06:04.319: E/AndroidRuntime(4319): Process: com.example.myapp, PID: 4319
01-05 11:06:04.319: E/AndroidRuntime(4319): java.lang.NullPointerException
01-05 11:06:04.319: E/AndroidRuntime(4319):     at com.example.myapp.CustomListAdapter$1.onClick(CustomListAdapter.java:71)

Please Help me out.

At first ,You can post your Logcat .

FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

FLAG_ACTIVITY_NEW_TASK

if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state.

You can use Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK

Finally ,It should be ,

   Intent intent = new Intent(ctx, NewActivityName.Class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_NEW_TASK);
   intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
   startActivity(intent);

Replace your activity name with new activity .

startActivity(new Intent(ctx,NewActivity.class));
((Activity) ctx).finish();

As i was starting an activity inside getView() so ((Activity) ctx).finish(); was not working. I just changed it to ((Activity)parent.getContext()).finish(); and it was working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM