Search code examples
javaandroidandroid-intentandroid-activity

how to not lose any data in the first activity when you use intent


Okay lets say we have 2 activities such as mainActivity(mA) and secondActivity(sA) mA is the user interface that takes name,number from the user. Also there is a textview and a button. The button leads to the sA. sA has a calendar that lets you select a day. You select the day and you come back to mA and the date you selected is written on the text view. Its okay till here now the problem is, when i come back to mA my previous data that i got from the user is wiped away its empty except my text view that i got the date. How can i save the user data before going to other activity and when i come back they'll stay.

I expect to see my data from the first screen when i come back from the second activity.

EDIT:

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), calendarActivity.class);
            startActivity(intent);
        }
    }

    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            String date = dayOfMonth + "/" + month + "/" + year;
            intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("date", date);
            startActivity(intent);
        }
    });

String date = getIntent().getStringExtra("date"); 
tarih.setText(date);

Solution

  • Try using startActivityForResult.

    Read the docs from the official Android Developer Docs

    Essentially what you would do is to pass data through objects called Intents like so (in Kotlin for instance):

    In your first activity:

    private fun goToSelectDateActivity() {
        val requestCode =324
        val intent = Intent(this, CalendarActivity::class.java)
        startActivityForResult(intent, requestCode)
    }
    

    In your Second activity

    private fun passSelectedDateToPreviousActivity(selectedDate: Date) {
        val data = Intent()
        data.putExtra("DATE", selectedDate.toString());
    
        setResult(Activity.RESULT_OK, data);
        finish()
    }
    

    Back in your First Activity override the method onActivityResult

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
         if(resultCode != Activity.RESULT_OK) return
         when(requestCode) {
             324 -> { yourTextView.text = data.getStringExtra("DATE"); }
             // Other result codes
             else -> {}
         }
    }
    

    These snippets are very quick and dirty snippets I'd suggest for instance that you have your requestCode as a static variable in Java or inside an attribute of the companion object for the Activity in Kotlin.

    The Java equivalent would be

    In your first activity:

    private void goToSelectDateActivity() {
        int requestCode =324;
        Intent intent = new Intent(this, CalendarActivity.getClass());
        startActivityForResult(intent, requestCode);
    }
    

    In your Second activity

    private void passSelectedDateToPreviousActivity(Date selectedDate) {
        Intent data = new Intent();
        data.putExtra("DATE", selectedDate.toString());
    
        setResult(Activity.RESULT_OK, data);
        finish();
    }
    

    Back in your First Activity override the method onActivityResult

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if(resultCode != Activity.RESULT_OK) return;
         switch(requestCode) {
             case 324: 
                 yourTextView.setText(data.getStringExtra("DATE"));
                 break;
             // Other result codes
             default: break;
         }
    }