Activity stack is not clearing after giving Cleartop - android

I am having 4 buttons at the bottom. Suppose A,B,C,D. These buttons I have created as a widget and including in every layouts. ON each button click I am opening one activity which I have implemented in Common class. When my application starts A will be on selected state, under A I am starting some activities A1, A2, A3. When I press B, B will be in selected state and B1 activity will be started. I am setting CLEAR_TOP flag when I start B1, but the activities I started under A is not clearing. When I click A button again A1 activity will be started and all the activities like A2 and A3(previously started) will be cleared. But B1, B2 are not getting cleared. why it is happening.
public class TabBar implements OnClickListener{
private Context fContext;
//private Activity fActivity;
private Button btnHome, btnReward, btnProfile, btnFav;
private Activity activity;
//private Button venues,orders,about;
public TabBar(Activity activity, Context context) {
this.activity = activity;
btnHome = (Button)activity.findViewById(R.id.btn_tab_home);
btnReward = (Button)activity.findViewById(R.id.btn_tab_rew);
btnProfile = (Button)activity.findViewById(R.id.btn_tab_profile);
btnFav = (Button)activity.findViewById(R.id.btn_tab_fav);
btnHome.setOnClickListener(this);
btnReward.setOnClickListener(this);
btnProfile.setOnClickListener(this);
btnFav.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_tab_home:
Intent int1 = new Intent(activity, HomeActivity.class);
int1.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int1);
activity.finish();
activity.overridePendingTransition(0, 0);
break;
case R.id.btn_tab_rew:
Intent int2 = new Intent(activity, Rewards.class);
int2.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int2);
activity.finish();
break;
case R.id.btn_tab_profile:
Intent int3 = new Intent(activity, ProfileActivity.class);
int3.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int3);
activity.finish();
break;
case R.id.btn_tab_fav:
Intent int4 = new Intent(activity, FavouritesActivity.class);
int4.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int4);
activity.finish();
break;
default:
break;
}
}

Note, Clear top stack, will clear all the activities of top of some activity from activity stack, if it is present in activity stack. If it is not present in activity stack, then it will push new activity B1 on activity stack.
In your case, Activity B1 is not present in activity stack, so it wont pop any activity from activity stack and will just push a new activity B1 on activity stack.
If you want to Implement TabBar, then you should use TabFragment or TabActivity for the purpose.

Related

back stack keeps adding when using Intents

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)
}

Why is my intent not passed when back button is pressed? [duplicate]

This question already has answers here:
How to pass data from 2nd activity to 1st activity when pressed back? - android
(8 answers)
Closed 4 years ago.
In Activity A, I can view a string. There is an EDIT button I press in Activity A that starts Activity B (and closes A). I pass the string with an intent from A to B in my onCreate code like this:
In Activity A I have:
//for the edit button
edit = (Button) findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//open the Edit Activity, pass over the string
Intent i = new Intent(ViewContact.this, EditContact.class);
i.putExtra("category", categoryname.getText());
//start Activity B
startActivity(i);
//close Activity A
finish();
}
});
In Activity B I have:
Intent i = this.getIntent();
category = i.getStringExtra("category");
//set the EditText to display the value of "category" key
categoryname.setText(category);
All works so far. But if my back button is pressed in Activity B to go back to A, I want to show the string again. As it stands, there is no value in the text box.
Back button code in Activity B:
//for the back arrow, tell it to close the activity, when clicked
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ViewContact.class);
i.putExtra("category", categoryname.getText());
//Start Activity A
startActivity(i);
//Finish Acitivity B
finish();
}
});
And then when Activity A starts again it should call the intent in onCreate with
Intent i = this.getIntent();
category = i.getStringExtra("category");
categoryname = (TextView) findViewById(R.id.textViewCategory);
categoryname.setText(category);
But instead the text box is empty.
Can you to tell me how I should go about passing the value back from B to A?
Just delete the finish() command

how to disable the previous activity when click on the next activity

I am having multiple activity in my program the flow of it is like MainActivity-LoginActivity-NextLoginActivity etc.,I am applying transparency theme on each activity except main activity so I want when I click any activity it should show MainActivity in background rather than previous activity.
Maybe you want result below?
You open app, app into these activity in order: MainActivity -> LoginActivity -> OtherActivity
And you want to click a button that in OtherActivity, you don't want to return the LoginActivity but MainActivity
If it is, you can do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
In this line: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Activity will use the Launch Mode by singleTask.
If activity stack has MainActivity, it will finish other activity that on top of MainActivity, then return MainActivity.
You can try to give an id to the parent layout in each activity and handle clicks on it.
Something like:
LinearLayout LoginActivity = (LinearLayout) findViewById(R.id.login_activity);
LoginActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, MainActivity));
//If you want to close this one
//this.finish();
}
});

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
}

How do you make a new activity start when a button is clicked?

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.
I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.
Can someone help me do this please? Thanks
EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?
So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.
public class MainActivity extends Activity implements View.OnClickListener {
// body
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.serviceBtn:
Intent serviceIntent = new Intent(this, ServiceActivity.class);
startActivity(serviceIntent);
break;
case R.id.searchBtn:
Intent searchIntent = new Intent(this, SearchActivity.class);
startActivity(searchIntent);
break;
case R.id.mapBtn:
Intent mapIntent = new Intent(this, MapActivity.class);
startActivity(mapIntent);
break;
}
}
Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().
EDIT for hawaii.five-0: here's how I'd do it:
#Override
public void onClick(View view) {
Class c = null;
switch (view.getId()) {
case R.id.serviceBtn:
c = ServiceActivity.class;
break;
case R.id.searchBtn:
c = SearchActivity.class;
break;
case R.id.mapBtn:
c = MapActivity.class;
break;
}
Intent i = new Intent(YourActivity.this, c);
startActivity(i);
}
EDIT2:
class CurrentActivity extends Activity
implements OnClickListener
{
void onCreate(Bundle b)
{
//Other initialization goes here...
((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
}
}
Its very easy dear. Just put this code on button click event
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
which is very easy to start a new activity when the button is clicked. I have given the example for this.
// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
// declare the Intent for moving another activity
Intent view = new Intent(YourCurrentClassName.this,
anotherClassName.class);
// startActivity is used to navigating the view
startActivity(view);
}
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);

Categories

Resources