queries on button operations of android - android

in the main activity of my app i have placed a button named as setting, if i click the button it moves to a new page where i have placed two buttons.
if i click in any of those buttons i want its corresponding operations to be done in another activity,how to do this...
in the same way if i click a button in Activity A, i need a button to be completely invisible in some other Activity...
how to do this....

The best way is to pass messages through the Intent that you use to start an activity. This answer gives a detailed code sample for this.

You can send some data along with the intent, which you used to start the other activity.
For eg, in Activity A
Intent i = new Intent(this, B.class)
b.setBooleanExtra("isButtonVisible", false);
startActivity(i);
Now in Activity B, in it's onCreate() method, use
boolean isButtonVisible=getIntent().getBooleanExtra("isButtonVisible);
if(!isVisible)
button.setVisibility(View.INVISIBLE) //Or do something with that
I think you got the idea now?

Related

How can i manage my activities and finish them properly?

I am new in android and I have total 6-7 activities in my application. I want to know how can I manage my activities properly means when I move to the A->B->C->D like that. Then how can I move that the stack of these activities not created.
On moving from one activity to the other I am using the below code:
Intent intent=new Intent(current.this,next.class);
startActivityForResult(intent, 0);
And now if I want to move back on the earlier activity I used the code as:
Intent start = new Intent(current.this,next.class);
startActivity(start);
finishActivity(0);
Is there a special reason that you don't want to use the activity stack and let the activities handle themselves?
The Android system has done a very good job with the activity lifecycle. It allows you to start an Activity from different places without confusing the user because the back button will bring the user back to a different activity.
If you don't have a very good reason to not use the Android guideline try to stick to the way the system is doing it. Every other thing will only give you problems.
You are starting activities for a result but how I understand you you will never return to them.
You can start an Activity and after that just finish the current Activity. That way the activity will not be put on the back stack. Now you need to listen for back button pushes and create the activities that you want to bring the user to.
If you want to move from Activity A to D like going to the start/home screen of you app you do the following:
Intent goBackToA = new Intent(context, StdActivity.class);
goBackToA.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(goBackToA);
The flag FLAG_ACTIVITY_CLEAR_TOP will tell the system that if the backstack contains an instance of the Activity this activity will be shown and all activity that are between the current activity and the target activity are removed from the backstack. This allows you to go back to a home activity without creating huge loops that the user can move through with the back button.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapear.
To move back to the previous activity you don't have to create a new intent, you can simply call this.finish() on the one that should dissapeear or you can press Back button to see the previous Activity .
whenever you want to navigate from one class to another use this code, may be this help you to navigate the Activity,
Intent nextpage = new Intent(CurrentActivity.this,NextActivity.class);
startActivity(nextpage);
this.finish();

Run multiple activity instances in Android from the single same class and move to the first instance on a button click

I have an activity A ,am calling the same activity again and again like A calls another A in turn it calls another A and so on.I am not finishing any of these activities.My problem is when I click a button in any of the inner A ,I want to go the A that was first created and need to remove all other A's on the top of it.How to do this?
Intent itent = new Intent(A.this,A.class);
startActivity(intent);
if you are using intents use like this
Intent b = new Intent(Xyz.this,
Abc.class);
b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(b);

OnClick restartActivity?

How can I restart An Activity on click?
For exemple: I have got in my AndroidManifest.xml 2 activities the activity A and B and they start when application starts...
But what I want is when I click in a button that is on Activity A it must restart activity B.
when you are in activity A, and proceeding to activity B, then your activity B automatically starts/re-starts
Why do you need to start both Activities on starting your app? When you say "restart", do you actually need to stop Activity B and start Activity B again? Or do you just want to show it? To start an Activity from another Activity, you could call something like this:
startActivity(new (Intent(this, ActivityB.class)));
The Android documentation gives plenty of detail. However, I think you should consider why you are starting two activities at once, and whether you might want to use a Service instead (not knowing any details of your app, I can't say).
Intent intent = new Intent(CurrentActivity.this, ActivityToLaunch.class);
startActivity(intent);
call above piece of code on onClick of view method.

How to go to the next layout after button click on main.xml and go to the next layout again in android

hi i would like to know how to i go to the next layout on android. i am new to designing apps and i managed to have main.xml with a button that has onclicklistener and r.layout.second.xml, that is done. now when i am at second.xml, i want to go to third.xml, however, when i click the button on the third.xml, it does nothing.
i have added a new activity.java with roughly the same code as the main.java file with exception that setContentView(R.layout.second);
i have added new activity to manifest however, when i reach the second.xml, i cannot click to third.xml.
any help please.
Layouts as you refer to them are tied to Activities (you can tie several layouts to a single activity by using ViewFlipper though).
What you're looking for is not to go to the second layout, but rather to move to the next activity.
You might have a button, where onClick executes the code to move to the next activity.
You'll need to use Intents to start a new activity, e.g.:
Intent nextActivity = new Intent(this, SecondActivity.class);
startActivity(nextActivity);
This will start the new activity, and by extension, use the second layout you're after.
As a test run, if you don't want to add a button yet, you could use the above code in your onCreate method, though it'll appear as though it's opened the second activity immediately. In reality, it'll open the first, and start the second before you can see it; you can press back on your device to show that the first activity was, in fact, created.
HTH

How to implement Back button functionality

I have 3 different activity namely, category,sub_category and list of items. In sub_category i created back button using intent Activity. Back button in list_of items should be focus to sub_category. i dont know how to do this.. pls guide me..
from category activity
call
Intent it=new Intent(category.this,sub_category.class)
startActivityForResult(it);
similarly
Intent it=new Intent(sub_category.this,list_item.class)
startActivityForResult(it);
and in onClickListener of back button simply finish your activity like
list_item.this.finish();
for going back to previous screen u can call finish() in your current activity. It will remove the current activity from activity Stack and take you to the previous one.

Categories

Resources