• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps A “clean” MainActivity pattern after the date has been changed

Hi, suppose that after activating the application, the date of 12.07.2019 will be displayed in the upper 'Current Date' header. After the introduction of the products for breakfast, I would like to change the date on 13/07/2019 by pressing imageButtonUp so that the same activity appears but without the entered data, so that I can introduce new products from 13.07.2019. After introducing the products on 13.07.2019, and then pressing the imageButtonDown button, I would like to return to the previous date, where the products I have introduced with the date 12.07.2019 are displayed. How could I achieve this effect? Do I need to connect ImageButtonDown and Up with MainActivity somehow? Or maybe I need to add some new activities? Because now if I introduce products and then press the button, the date changes but the products stay the same. Thank you in advance for each clue, because I have no idea how I could do this task for my project.

For a better illustration of the situation, I present my main_activity.xml:


I does not have ready code for this situation with adding products to meals, but ... I wanted to test this situation on a more trivial example, where the analogical mechanism takes place, and then move the solution to the project with the addition of products. Here is an example on which I tested the solution:

1. So this is my main_activity.xml:



2. This is my MainActivity.java:

Java:
public class MainActivity extends AppCompatActivity {

    TextView textView, textView2, textView3;
    ImageButton imageButtonDown, imageButtonUp;
    Calendar calendar;
    String formattedDate;
    SimpleDateFormat dateFormat;
    Button button;
    private static final int REQUEST_CODE = 1;

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



        textView = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        imageButtonDown = findViewById(R.id.imageButton1);
        imageButtonUp = findViewById(R.id.imageButton2);
        button = findViewById(R.id.button2);

         calendar = Calendar.getInstance();

         dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
         formattedDate = dateFormat.format(calendar.getTime());

         textView.setText(formattedDate);


        imageButtonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                calendar.add(Calendar.DATE, -1);
                formattedDate = dateFormat.format(calendar.getTime());
                textView.setText(formattedDate);
            }
        });

        imageButtonUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                calendar.add(Calendar.DATE, +1);
                formattedDate = dateFormat.format(calendar.getTime());
                textView.setText(formattedDate);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, REQUEST_CODE);
            }
        });

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {

            Information information = new Information(i.getStringExtra("day"), i.getIntExtra("deegres", 0));

            textView2.setText(information.getDay());
            textView3.setText(String.valueOf(information.getDegrees_Celsius()));
        }
    }

}

3. This is my second_activity.xml:



4. This is my SecondActivity.java:

Java:
public class SecondActivity extends AppCompatActivity {

    EditText editText1, editText2;
    Button button;

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

        editText1 = findViewById(R.id.editText);
        editText2 = findViewById(R.id.editText2);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                sendInformation();
            }
        });

    }

    public void sendInformation() {

        String day = editText1.getText().toString();
        int deegres = Integer.parseInt(editText2.getText().toString());

        Intent i = new Intent();
        i.putExtra("day", day);
        i.putExtra("deegres", deegres);

        setResult(RESULT_OK, i);
        finish();
    }
}

5. Additional class Information:

Java:
public class Information {

    private String Day;
    private int degrees_Celsius;


    String getDay() {

        return Day;
    }

    Information(String Day, int degrees_Celsius) {

        this.Day = Day;
        this.degrees_Celsius = degrees_Celsius;
    }

    int getDegrees_Celsius() {

        return degrees_Celsius;
    }
}

In this analogous example, after activating the application, the date of 12.07.2019 will be displayed in the upper header. After pressing Go to SecondActivity, I want to go to the second activity, enter information about the day and degrees Celsius, and then press the button Send information, which will help me return to MainActivity. The information will be displayed in textViews. Then, after pressing the imageButtonUp button, I would like to change the date on 13.07.2019 and receive the same activity, but with the cleared information in order to enter new data from the next day. Finally, after pressing imageButtonDown, I want to return to the earlier date 12/07/2019, but I would like to see the information that was introduced on that day.

Thank you in advance for every clue, because I'm stuck in place ..
 
Back
Top Bottom