3

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.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
manjari
  • 183
  • 1
  • 14

3 Answers3

4

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);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    Sorry , Its Not working . After pressing back button of device that activity is again displaying. – manjari Jan 05 '16 at 11:51
  • Its giving error this "The method finish() is undefined for the type new View.OnClickListener(){}" – manjari Jan 05 '16 at 12:37
  • @manjari Ji . Its okay .Can you post your total code ? you are getting now `NullPointerException` – IntelliJ Amiya Jan 05 '16 at 12:39
  • 1
    Thanks . It was solved . As i was starting an activity inside getView() so only finish() did not work. I just changed it to ((Activity)parent.getContext()).finish(); – manjari Jan 05 '16 at 12:44
3

Replace your activity name with new activity .

startActivity(new Intent(ctx,NewActivity.class));
((Activity) ctx).finish();
Aditay Kaushal
  • 1,021
  • 7
  • 17
1

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

manjari
  • 183
  • 1
  • 14