back stack keeps adding when using Intents - android

I have an activity A which uses a fragment A which has a list. The activity A can call on a search activity. The problem that I am seeing is if I were to go the search activity and then go back to the activity A, from there fragment A gets loaded. If I select an item from Fragment A i get taken to Fragment B, if i were to press the back button i have to click it 2 or 3 times. Any ideas? Do i need to start the search activity with a parameter so it does not add to the back stack. I have tried the Flag to Intent.FLAG_ACTIVITY_CLEAR_TOP when starting the search activity but the issue occurs.
Java:
Activity A:
public void popFragment(){
if(getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
getSupportFragmentManager().executePendingTransactions();
}
}
Fragment A:
private void showSearchActivity(){
Intent intent = new Intent(context, SearchActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent, 0)
}

Related

How to navigate from fragment in one activity to fragment in another activity?

I want to add back navigation to toolbar. I need to get from a fragment in an activity to a specific fragment in another activity.
It looks a little like this, where every orange line means navigating to a new activity or fragment:
How do I move from fragment B to fragment A from OtherActivity?
Consider these steps:
From Activity 1 holding Fragment A , you want to directly load Fragment B in Activity 2.
Now, I am thinking first, then you press a button in Fragment A, you can directly go to Activity B.
Then it means, you can simply load Fragment B as soon as you arrive in Activity 2.
Since you are dealing with back navigation (I believe you mean the upNavigation?), you can override the following:
But watch clearly, because if you need to load an exact fragment in Activity 2, you need to know somehow:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("frag", "fragmentB");
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
As you can see, when you click the back arrow on the toolbar, we pass a value through our intent to identity which fragment we want to load.
Next, in your Activity2, simply get the intent extra and do a switch or an if statement:
#Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
String frag = intent.getExtras().getString("frag");
switch(frag){
case "fragmentB":
//here you can set Fragment B to your activity as usual;
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentB()).commit();
break;
}
}
From here, you should have your Fragment B showing in Activity 2.
Now you can handle the same thing while inside Activity 2 to decide where to go when a user clicks the back home arrow!
I hope this helps you get an idea.
Note: I thought about the interface approach and realized it is not necessary since this can be done easily with this approach!
To navigate from one Activity to another Activity's Fragment, with Kotlin version 1.4.0 and, for example, calling a click listener on a button it works so:
binding.yourButton.setOnClickListener {
supportFragmentManager.beginTransaction().replace(R.id.yourLayout, NameOfYourFragment()).commit()
}
Use this code to change your fragment
fragmentManager.beginTransaction().replace(R.id.container_body, new FragmentC()).commit();
and to show navigation on custom toolbar add
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true)
((AppCompatActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(true);

Home button parent fragment-child activity

I am having Activity A and Two Fragment called Fragment A and Fragment B.
In Fragment B List View is implemented. On Clicking of any list item the new activity get instantiated (Activity B). The Problem with the scenario 2
if the user pres the home button and again resume the activity then activity 2 is getting resumed.After resuming activity if the user press the back button then activity is getting into pause stage instead of returning back into to the parent fragment(Fragment 2).
Manifest for Activity 2
<activity
android:name=".activity.Activity2"
android:label="#string/label1"
android:parentActivityName=".activity.Activity1"
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.activity.Activity1" />
</activity>
Let me know what's causing this behavior. How can i retain fragment B when Activity B move into pause state.? or any other solution?
You could override the onBackPressed of your second activity, so that you always return to the first activity with fragment B shown.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("some tag", "some text");
startActivity(intent);
}
And in your first activity do something lke that:
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.getStringExtra("some tag").equals("some text"))
{
methodToDisplayFragmentB();
}
}
Hope this helps.

how to clear the fragment stack and activity stack on a button click

There are similar questions, but none of the are solving my issue.
My app flow is following:
Activity home starts Activity B(which does the setup work)
In activity B hosts three screens as fragments...
Fragment1 -> fragment2-> fragment 3.
This is how I make fragments. I am not using replace.just adding it.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = null;
if (getFragType().equals("simple")) {
fragment = new SimpleFragment().newInstance();
}
if (getFragType.equals("inter")) {
if (getFragType.getComponent().equals("con"))
fragment = new SetupConFragment().newInstance();
else if (getFragType.getComponent().equals("ben"))
fragment = new SetupBenageFragment().newInstance();
}
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
Fragment3 has a done button. So once all the 3 steps are done from fragment 3 am launching a new activity C.
The issue is:--
From the activity C , if the user presses the back button, it diplays fragment 2, then fragment 1. Ideally once the user is in Activity C, back press should just exit the screen
I have used all combinations of the intent flags, but it is not helping.
And this is on my button click.
Intent launch = new Intent(mContext, MainActivity.class);
launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launch.putExtra("Exit me", true);
mContext.startActivity(launch);
((ConfigureActivity)mContext).finish();
Any help will be appreciated.
You can do this way:
Clear Fragment stack:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
Clear Activity stack:
Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Hope this will help you.
Say, if you want to start Activity B in Activity A but you don't want user to go back to Activity A,
final Intent intent = new Intent(this, ActivityB.class);
startActivity();
finish(); // kill the current activity (i.e., Activity A) so user cannot go back
In your Activity C, you can control Back key by overriding onBackPressed().
#Override
public void onBackPressed()
{
// code here depending on your needs
}

Navigate up from activity back to fragment

Let's say I have two activities called A_Activity and B_Activity. A_Activity has 3 fragments named A_Fragment, B_Fragment, and C_Fragment. A_Activity acts as a base activity for the three fragments.
From B_Fragment, it is possible to navigate to B_Activity and from B_Activityit is possible to navigate up to B_Fragment. from A_Activity it is possible to navigate to A_Fragment, B_Fragment, orC_Fragment. Lastly, from A_Fragment, B_Fragment, or C_Fragment, it is possible to navigate to any other fragment.
Here's an arbitrary representation of the hierarchy of activities and their respective fragments:
A_Activity
A_Fragment
B_Fragment
B_Activity
C_Fragment
Let it also be a given that A_Fragment is the default fragment for A_Activity.
My current solution to this is: in B_Activity's home icon selected, I am creating a new intent:
Intent intent = new Intent(B_Activity.this, A_Activity.class);
intent.putExtra("data_to_start_B_Fragment", "B"); // instead of starting A Fragment
startActivity(intent);
What is the proper way to navigate to B_Fragment from B_Activity?
From B_Fragment, starting B_Activity using startActivityForResult(...) by getApplicationcontext not getActivity().getApplicationContext().
In Activity B, you use setResult(data here) to return fragment and finish ActivityB to back fragment B.
From B_Activity to B_Fragment (if you start B_Activity from B_Fragment), you just need to detect action from either home button or system back button and then kill the Activity by calling
finish()
e.g. in B_Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

Android, start new activity using same activity class, is this possible?

i want to create an app with navigation, wiyh navigation flow like this
ListView Activity -> Detail Activity (item 1) -> Detail Activity (item 2)-> Detail Activity (item 3) -> ... and so on
is this posible? how to achive this?
Edited
Sorry for being not clear,
Lets say i have activity with ListView, then when i tap one item, it will start new activity which contain detail information of selected item, and from those activity, there are next / previous button, and when i tap it, it will start new activity using same class and layout, but with information of next or previous item of ListView previously selected item.
Oh and also I need that user can return to previous screen on each backkey, so when user tap back key the activity will go like this
Detail Activity (item 3) (finish) -> Detail Activity (item 2) (finish) -> Detail Activity (item 1) (finish) -> ListView Activity
Do this way
Yes this is possible to call Activity call it self, because Every activity is different process in android.
Activity stack will maintain by Android Don't worry about that.
public class ListActivity extends Activity {
// call DetailAcitvity
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item", "item1");
startActivity(intent);
}
public class DetailActivity extends Activity {
// Calling itself
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item", "item2");
startActivity(intent);
}

Categories

Resources