1

In a project there will be lots of pages(activities) and user will be able to switch between these pages (activities). So when user press the corresponding button that opens page_2 from page_1, I need to create a new Activity. However, if user comes back to page_1 and try to open the page_2 again, there will be a new Activity created again, instead of opening the previously created activity ( I want user to see the page_2 as he/she left it without anychanges). So I want to put something like

if(SecondActivity==null)
{
//Create new activity
}
start(new_activity);

Here is the corresponding code ( I couldn't implement onClickListener because I couldn't disable it in onPause() method... so I used onClick from xml)

public class MainActivity extends Activity {


    private View.OnClickListener openSecondPage = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button button_newPage = findViewById(R.id.button_newpage);
            button_newPage.setText("Clicked");
            Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
            startActivity(secondPage);

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


    }

    public void onResume(){
        super.onResume();
        //Button button_newPage = findViewById(R.id.button_newpage);
       // button_newPage.setOnClickListener(openSecondPage);
    }

    public void onPause(){
        super.onPause();
        //Destroy the on click listener
        Button button_newPage = findViewById(R.id.button_newpage);
       // button_newPage.setOnClickListener(null);

    }

   public void openSecondPage(View v)
    {
        Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
        startActivity(secondPage);
    }
    }

Edit: Here is the new code with Flags:

MainActivity.java code:

public class MainActivity extends Activity {


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

    }

    public void onResume(){
        super.onResume();

    }

    public void onPause(){
        super.onPause();
    }

   public void openSecondPage(View v)
    {
        Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
        secondPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(secondPage);
    }
    }

SecondActivity.java code:

public class SecondActivity extends Activity {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);


    }
    protected void onResume()
    {
        super.onResume();

    }

    public void goBack(View v)
    {

    }

    public void goMainPage(View v)
    {
        Intent mainPage = new Intent(getApplicationContext(),MainActivity.class);
        mainPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        startActivity(mainPage);
    }
   
}
  • 1
    What I have understand from your question is that you want to save the inputs or any user provided data in the second activity. If user comes back again to second activity. Correct ? – Muhammad Awais Jan 26 '21 at 11:01
  • You can use the finish() method that will destroy the current activity and show the previous one. – KotlinIsland Jan 26 '21 at 11:07
  • @MuhammadAwais That is correct. But that does not look possible with creating "new Activity" when everytime the button is pressed. So I think I need a way to know if activity is also created/started, if it did then I just need to open it without creating it again. – Günkut Ağabeyoğlu Jan 26 '21 at 11:08
  • @KotlinIsland But I don't want to destroy the activity. I want to return to that spesific activity later on, without losing it's content. – Günkut Ağabeyoğlu Jan 26 '21 at 11:09
  • 1
    I understand. Check this thread : https://stackoverflow.com/questions/9937120/switching-between-activities-in-android – KotlinIsland Jan 26 '21 at 11:10
  • @KotlinIsland From the link, somebody mentioned "viewFlipper" I think I should go with it instead of creating an Activity for every Page I have – Günkut Ağabeyoğlu Jan 26 '21 at 11:15
  • 1
    But if you use the flag FLAG_ACTIVITY_REORDER_TO_FRONT then the activity is not created again but it is brought to front when you call startActivity. – KotlinIsland Jan 26 '21 at 11:17
  • 1
    Günkut :: You will have to save the input data either in Room or SharedPreferences. Then, whenever you come in `SecondActivity`, you have to check, if there is data available show that to the user, else make all the fields default. – Muhammad Awais Jan 26 '21 at 11:23

3 Answers3

1

I think you can resolve problem by FLAG_ACTIVITY_REORDER_TO_FRONT flag. You need to set flag when start activity.

public void openSecondPage(View v) {
    Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class);
    secondPage.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(secondPage);
}

You can find more detailed information in this link.

https://developer.android.com/guide/components/activities/tasks-and-back-stack

Daniel.Wang
  • 2,242
  • 2
  • 9
  • 27
1

Show Activity A, then when you need it then show Activity B, then when you need it then show Activity A again, by calling startActivity + setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) => Activity A will show up as it was exactly before showing Activity B

KotlinIsland
  • 799
  • 1
  • 6
  • 25
  • Thanks. By showing the activity B do you mean : First create a new Intent, then setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) and then startActivity(ActivityB) – Günkut Ağabeyoğlu Jan 26 '21 at 13:03
  • 1
    No, the flag must be set from Activity B when you call again startActivity to show Activity A. – KotlinIsland Jan 26 '21 at 13:05
  • What if I have like 5 Activities such as : A -> B -> C -> D-> E.. Would using flags be useful in that case too? – Günkut Ağabeyoğlu Jan 26 '21 at 13:18
  • 1
    Yes it will be useful in this case too... For going from any activity to any other one. This flag means "Use existing activity that has been created before and bring it to front", *instead of* "Create a new activity even if one has been created before". Activities are like a stack of sheets. – KotlinIsland Jan 26 '21 at 13:45
  • Sorry for asking similar questions over and over again. But I couldn't find any useful examples or a good documentations that explains the concept in depth over the internet. So I hope that is my last question. When I use flag as you have suggested, states of Toggle Buttons get zeroed. I added the new code to the main questions under the edit part. Can you take a quick look at it please? – Günkut Ağabeyoğlu Jan 26 '21 at 13:54
  • 1
    Can you try with FLAG_ACTIVITY_NEW_TASK instead ? Sorry too, I had the same problems as you in the past. I know that you're not far from the solution. https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_NEW_TASK – KotlinIsland Jan 26 '21 at 14:11
  • Using FLAG_ACTIVITY_NEW_TASK instead of Intent.FLAG_ACTIVITY_NEW_TASK, still zeroes the state of the toggle buttons on return to the activity from another activity unfortunately. – Günkut Ağabeyoğlu Jan 26 '21 at 14:24
  • 1
    FLAG_ACTIVITY_REORDER_TO_FRONT works now! Thanks for the help again. – Günkut Ağabeyoğlu Jan 26 '21 at 14:27
  • 1
    Yeah well actually both of you were right... Wish I could accept both answers – Günkut Ağabeyoğlu Jan 26 '21 at 14:31
  • 1
    Accept his answer and upvote my comments :) – KotlinIsland Jan 26 '21 at 14:35
0

According to your question asked, it seems you have to use either Room or SharedPreference in SecondActivity to save the user input data.

So, whenever you come back to SecondActivity, check if there is already data available. If there is a data available, show it to the user else set the fields with default values.

Muhammad Awais
  • 448
  • 5
  • 17
  • Thanks for the answer. And how can I do that? – Günkut Ağabeyoğlu Jan 26 '21 at 11:32
  • For example :: make any model class, containing data members same as the input fields of your form. Then, save model class as `Json` in `SharedPreferences` using `Gson`. Check this answer: https://stackoverflow.com/a/18463758/9144811 – Muhammad Awais Jan 26 '21 at 11:35
  • 1
    No, that's not the solution. Once an activity is created then every data are saved automatically, so only using FLAG_ACTIVITY_BROUGHT_TO_FRONT is the solution to that. And for passing data between activities you can use Intent.putExtras. – KotlinIsland Jan 26 '21 at 11:44
  • I see, thanks. Instead of using SharedPreferences, I can use save instance state I in onPause then use that Bundle in onResume when I resume the activity. – Günkut Ağabeyoğlu Jan 26 '21 at 11:45
  • 1
    Show Activity A, then when you need it then show Activity B, then when you need it then show Activity A again, by calling startActivity + setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) => Activity A will show up as it was exactly before showing Activity B. – KotlinIsland Jan 26 '21 at 11:49