Search code examples
javaandroidandroid-activityonclickbuttonclick

Can I create a new Activity on the first click of a button and after the first click open the previously created activity?


I've been developing a Grade/GPA Calculator Android application during my free time and I'm wondering if there's a way to do the following:

In my MainActivity I'm letting the user add as many Semesters as they want and the idea is that when a user clicks on, for example, Fall Semester 2020 the Activity will change to the Activity that will have the Classes the user is taking during that specific semester. My question would be, is there a way I can do that? Because I thought of creating a SemesterActivity, but the problem with that would be that it will always redirect to the same Activity so it will always have the same content so it doesn't matter if the user clicks on Summer Semester 2020 or Fall Semester 2020 it would open the same activity with the same classes that the user had added.

I've been doing some research online about creating an activity on the click of a button, but I've had no luck and I'm not sure if that's really what I should do. If anyone can point me in the right direction I would appreciate it. Thanks!

Edit: Maybe this video might help with what I want to do. As you can see, I click on a specific Semester and it redirects to an empty Activity that has a button that lets the user add new courses and that's what I want to do, but I want to do one for each semester


Solution

  • You would need to use Extra to pass data to SemesterActivity to tell it what semester to render data accordingly. To do that in MainActivity:
    
    Intent myIntent = new Intent(this, SemesterActivity.class);
    myIntent.putExtra("semester","Summer2020");
    startActivity(myIntent);
    
    in SemesterActivity, you retrieve the Extra and determine the semester:
    
    Intent myIntent = getIntent(); 
    String semester= myIntent.getStringExtra("semester");