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.
Related
In my application I have 6 activities like 1->2->3->4->5->6. In 6th activity user can choose any activity(I am launching with intent). Means he can choose 1 or 2 or 3 or 4 or 5. Now when I press back button it is coming back to 6th activity again(which I do not want).
ex:
I have executed like this 1->2->3->4->5->6. Now I am in 6th activity. Here I have button, when I press it, it launched 4th activity. My code is like this on button click.
Intent intent = new Intent(MainScreen.this, UniversitiesScreen.class);
startActivity(intent);
So now I am in 4th activity. When I press back button it taking me to 6th activity(which I came from) instead 3rd activity(what I am expecting).
Can any one help me to handle this type of navigation.
Simple answer is edit your onBackPressed method in every activity...
For example, Write in 4th activity like this
#override
public void onBackPressed(){
Intent in = new Intent(this, ThirdActivity.class);
startActivty(in);
finish();
}
Please check the context that you are passing to intent. Every activity is different so pass the activity context from where you are calling another activity.
If my guess is true Mainscreen.this is first activity, and if you call fifth activity from fourth activity, give intent as
Intent in = new Intent(FourthActivity.this,FifthActivity.class);
startActivity(in);
finish();
And you are not saving any data in backstack and you are going back manually...
The best way is to use the NavUtils.navigateUpFromSameTask(this) function in your activity. For this you need to define the parent activity in the manifest for each activity so when you are in the 4th activity then the parent activity is the 3rd activity.Then when you click on back or navigate up you end up in the parent activity i.e. from 4th to the 3rd.
Take a look at this
http://developer.android.com/training/implementing-navigation/ancestral.html
Look at the manifest and then the Navigate up to parent task section
This will also help in returning to the same state of the 3rd activity from the 4th activity.
I have a ListView with some buttons lets say every list has button_1 and button_2 when i click button_1 i will go to next intent but will not finish() the previews activity and then on my present activity there is a button_3 and if i click button_3 the button_2 from previous activity will change too when i go back. now my question is is there a way we can pass a view from another activity or is it possible to call setNotifyOnChange() on another activity?
just call setNotifyOnChange() in onResume() method of the previous activity
I had created an android app which has 3 activity (A,B,C) in activity A there is 2 autocomplete textview which fetch data from database and button after selecting data in autocomplete text user press button and it goes to next activity which show listview.
Now the problem is when I press the devices back button from listview is display activity A with selected data in autotext how can I avoid the selected data when I press device back button.
Thanks for helping in advance
When you press the back button, activity A will "resume". So, in your Activity A override "onResume"...in the onResume method, clear your fields.
There is nothing with the listactivity's back button as you are trying to clear you field in the previous activity. As you are not finishing your previous activity while creating the list activity so the previous activity is not changing. So while you are returing from the list you are saying the same data as before. to do so as #Dave suggested you could have done the clearing in the oResume method or onPause method. But there is a problem. If you clear your data in onResume or in onPause you data will be cleared for other pausing or resuming reasons like pressing home, or for other applications etc. So you can do any of two
Option 1:
clear the data of the previous activity when you are starting the list activity
or
Option 2:
instead of startActivity call startActiviyForResult and also override onActiviyResult method. So there you can detect when you are returning from list activity. Then clear the data.
To deal with this problem, I would simply override onBackPressed() method, and in there, I would have cleared activity stack as there is no logical explanation to keep activities in memory, and then start a fresh intent to your original activity.
Here is code.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity_A.class); //I suppose they are in same package
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
I hope it helps.
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
this also works well..
you should implement startActivityforResut
When you come back to this activity on result you can clear the data
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?
I am totally new to android development....what piece of code shall I write to move back to the last activity from the current activity on a button click?
I use intents to switch between the activities but is there anything specific to resume back on the last activity?
Please advise...
Thanks,
Pressing the back button should do that "out of the box"...
If the user clicks on Back button, application will automatically take you to previous Activity. You don't need to do anything.
If what you pretend is to implement that action from current Activity, for example through a Back menu option, just call finish() in your Activity. You can call setResult before that, if you called that activity with startActivityForResult
Ger
By default back resume previous activity.
You should read this to better understand android Activities:
http://developer.android.com/guide/topics/fundamentals.html#lcycles