I am having following code :
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnSrch){
Intent i = new Intent(this, SearchByCode.class);
View view = SearchGroup.group.getLocalActivityManager()
.startActivity("SearchByCode",i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
SearchGroup.group.replaceView(view);
}
}
But when I am clicking the button it shows the next screen abruptly. I want to show the slide-in animation here.
I can't quite tell but I think you want to know how to launch another activity called SearchByCode from your current activity.
To do this you need to create an Intent object, optionally add flags or bundles to the intent, then call startActivity(Intent).
Here is a code sample:
Intent i = new Intent(this, SearchByCode.class)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(i);
Related
I have an Android app that displays elements in a GridView: each of these elements is clickable and starts an Activity with its details; then you can go through another activity from the second one to add more data.
My question is: when I go back from 3rd to 2nd activity, my app crashes (and I know this is because going from 3rd activity to 2nd one, the 2nd so called hasn't got the intent data that it needs).
What can I do to solve this issue?
My Gridview calling the 2nd activity
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), PokemonDetails.class);
i.putExtra("id", position);
startActivity(i);
}
});
My 2nd activity calling the 3rd:
pokeDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MyPokeDetails.class);
startActivity(i);
}
});
you can just override the onBackPressed() method with the same function of your buttons.
#Override
public void onBackPressed() {
//put Intent to go back here
}
Your activity should be recreated from the last state when you go back to it. Do you check the intent you have your data in in your 2nd activity to be non null? I guess the app could crash because of that.
You could also work with the savedInstanceState.
Override onSaveInstanceState and put the id you need into the bundle. If onCreate of your second Activity gets called, look if Bundle is non null and go get your value.
Further info: https://developer.android.com/training/basics/activity-lifecycle/recreating.html
You should use startActivityForResult() instead of startActivity() For details see this answer and Official documentation
I have an Activity named A,
in this, a ListView and one button are there.After clicking on this button,List View is shown and from this list view, by clicking on a its items, I can move to Activity B.
Now the problem is this when I come back from Activity B -> Activity A then, I see the Button only not the list view.
because I am calling intent of Activity A..
Code
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i;
i = new Intent(B.this, A.class);
startActivity(i);
finish();
super.onBackPressed();
}
In Activity B, I have the above implementation and i am using finish() in On Pause() condition also.
I want to see the List View with buttons.
Do i need to call whole code again to show the ListView or is there any other way to resolve this problem??
Is there any way to save the previous activity view?
Its because you are finishing Activity A before switching to Activity B. If you want your ListView as it is then do not finish Activity A and then try. If still your data does not visible then bind your listview again in onResume() of Activity A.
//removed finish().
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i = new Intent(B.this, A.class);
startActivity(i);
super.onBackPressed();
}
You are creating your activity aagain since u hv called the finish
method and when u go back it calls onCreate rather it should call
onResume method of lifecycle.
Just do one thing.rmove finish() from your code
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent i;
i = new Intent(B.this, A.class);
startActivity(i);
super.onBackPressed();
}
onResume called when activity start or when you return from another activity as per life cycle of Activity, its better to keep your code inside onResume, which will refill your listview with data.
SO when you called activity B from A (in Activity A fill your listview inside onResume method) and when you press back, its again callon onResume and fill your listview.
Instead# doing code inside onBackPressed of activity B.
In my case I have several Tabs within TabHost and 1st tab should be able change it's view without navigation from TabHost (to be able to immediately access other tabs).
Is it possible to dynamically change Tab view for TabHost?
I have the same situation, my solution : get current tab, and setOnClickListener.
Example:
View tab = (View) tabHost.getCurrentTabView();
tab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
finish();
}
});
cheers :)
I really want to move from an activity within a tab view to another activity within the same tab.
public void onClick (View view) {
// This creates an intent to call the 'Called' activity
i1 = new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
try this...
TabSpec spec=...//our tabSpec
Intent intent = new Intent(this.getBaseContext(), Called.class);
spec.setContent(videosIntent);
I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here