0

I have the following 2 activiteis:

  1. MainActivity
  2. TempActivity

TempActivity

public class TempActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temp);
    }
}

In MainActivity, I have the following code:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

                List<Sms> lst = getAllSms();
                Intent iti=new Intent(getBaseContext(),TempActivity.class);
                startActivity(iti);

            }


        }
    });
}


public List<Sms> getAllSms() {
    List<Sms> lstSms = new ArrayList<Sms>();
    Sms objSms = new Sms();
    Uri message = Uri.parse("content://sms/");
    ContentResolver cr = this.getContentResolver();

    Cursor c = cr.query(message, null, null, null, null);
    this.startManagingCursor(c);
    int totalSMS = c.getCount();

    if (c.moveToFirst()) {
        for (int i = 0; i < totalSMS; i++) {

            objSms = new Sms();
            objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
            objSms.setAddress(c.getString(c
                    .getColumnIndexOrThrow("address")));
            objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
            objSms.setReadState(c.getString(c.getColumnIndex("read")));
            objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
            if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                objSms.setFolderName("inbox");
            } else {
                objSms.setFolderName("sent");
            }

            lstSms.add(objSms);
            c.moveToNext();
        }
    }
    c.close();

    return lstSms;
}

So, on click of button (textView), I am reading sms from device and once this is done, I am launching the TempActivity. This works fine and activity is launched. But when in my TempActivity I press back button, the app crashes and I get the following error:

Unable to resume activity {com.myProject/com.myProject.MainActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
helloworld
  • 1,002
  • 3
  • 15
  • 27

2 Answers2

2

try to remove this line

this.startManagingCursor(c)

This method was deprecated in API level 11, I think problem is this line and you don't need this function at all. This method try to manage your Cursor but it not aware for life cycle and crash happen

And it is better to don't use FLAG_ACTIVITY_CLEAR_TOP and BaseContext

Instead of that Use this

Intent iti=new Intent(MainActivity.this,TempActivity.class);
iti.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

Try setting your cursor to null after you close it.

c.close();
c=null;
Alan Godoi
  • 657
  • 1
  • 12
  • 39