I'm making an Android app for a School.
1st activity is of Student which contain a ViewPager consists of 3 Fragments. This activity is expensive to load as it contains 3 Fragments.
1st Fragment contain a ListView of Games
2nd Fragment contain a ListView of Cartoons
3rd Fragment contain a ListView of Test (ListView contain Only Title of Test).
Now in Test Fragment onClick on of test (displayed in listview) a new ShowTest Activity is open which displayed the complete test.
Every thing working fine.
That's the code in TestFragment to start ShowTest Activity
test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent it = new Intent(getActivity(), ShowTest.class);
startActivity(it);
}
});
And in ShowTest Activity I'm just calling finish();
public void onBackPressed() {
this.finish();
}
But in ShowTest when onBackpressed() is called Student activity re-creates (Every thing in fragments reload). I don't want this activity to be created 2nd time. As it expensive to load.
These two links didn't helped me
Open Info Activity without closing Main Activity
Android - Relaunching an activity without having to re-create it
Your Help will be appreciated.
Thanks (Edited)
You might be calling finish() on start of other activity. Dont call that and start activity without finish().
Sorry, My mistake
In Android Manifest
<activity
android:name="plan9.naseemdev.muhammadtaimoor.naseem.Student"
android:configChanges="orientation|screenSize"
android:noHistory="true" />
Due to '
android:noHistory="true" '
Student activity was re-creating every time.
After removing this line it work for me.
Share your code so that i can help further.
Also please check you are initializing the pager adapter properly.
Related
I would like to design a feed-based app with similar design like Facebook, Twitter or Instagram. In the main screen, I have a FeedActivity which holds a ViewPager for holding 3-4 fragments (represented as tabs)
FeedActivity
++ ListOfItemsFragment (there is a ListView holding posts)
++ OtherFragment
++ SomeOtherFragment
I would like to open another activity (or maybe another fragment in ViewPager) when a user touches a post (listed in ListOfItemsFragment). If I open this new activity (PostDetailActivity) and when I return to the FeedActivity, all data is lost in the ListOfItemsFragment because FeedActivity is created again.
Should I create a new Activity for the post detail, or should I add another fragment to FeedActivity independent of ViewPager (I want this fragment to be a popup, not a part of the tabs).
How can I support this behaviour?
Thanks.
Every time you press back the new Activity instance is created. Hense filled data erases. Just type the following lines in manifest file.
<activity android:name=".MainActivity"
android:launchMode="singleTop">
</activity>
Looking at the problem from a different perspective, sqlite is a solution.
Plus, there's an added bonus of storing states even if your app is completely destroyed and has to restart. See this SO thread.
Simply override this method and store that data that you want to display again
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("intTag", intValue); // Save all the value that you want to retain
super.onSaveInstanceState(outState, outPersistentState);
}
and in you onCreate check if the savedInstanceState is not null than get the value and update your
if(savedInstanceState != null) {
int data = savedInstanceState.getInt("intTag");
// Retrieve all the data you have settled there
}
I am making an application with tabs. In that app same TabView is to be shown in multiple activities in hierarchy. For that i used ActivityGroup.
In my application i can navigate from first activity containing tab to its child activity and can come back to previous activity by pressing a button in child activity. While navigating between these two activities, i get StackOverflowError after few navigations.
I tried flag
Intent.FLAG_ACTIVITY_CLEAR_TOP
but it doesn't help.
I also tried
finish()
but it finishes whole ActivityGroup.
Then i tried method
finishActivityFromChild()
but still getting same error.
This is my code for moving from first activity containing tabs to its child-
intent = new Intent(context, ChildActivity.class);
View view = getLocalActivityManager().startActivity("activity2", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
setContentView(view);
finishActivityFromChild(getCurrentActivity(), 0);
And the same code i am using for coming back to parent activity on click of a button-
public void onClick(View arg0) {
intent = new Intent(context, ParentActivity.class);
View view = getLocalActivityManager().startActivity("activity1", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
setContentView(view);
finishActivityFromChild(getCurrentActivity(), 0);
}
Now i have no idea what to do for this problem. Any help is appreciated.
Thanks in advance.
I think some code would help here. If I were you, I would try to plent printouts of functions called, to see what function is being called recuresively that might cause the stack overflow. (Some breakpoints might also do the trick)
[Update Solution]
Referring to the post in the link
ViewPager PagerAdapter not updating the View
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
// And inside my PagerAdapter
#Override
public int getItemPosition(Object object) {
return 0;
}
Fixed all my problems, i just used Intent.setAction().
[Update to below Post]
My problem is i have a ViewPager PagerAdapter in my Main Activity. On clicking one of the 3 buttons., any specific intent will be fired (i used intent, i could have gone with just State variable as well, just that i pass some Uri with the intent). What happens is., i do
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
This is why i was guessing maybe i should just do startActivity again, with the new Intent Action on the same Activity.
Spawning a new Activity, i would have to redraw every other view in the layout which is basically the same code, for the most part in the current activity.
Any suggestions here? Please help
[Original Post]
I have a PagerAdapter and 3 Buttons in the my Main Activity. This activity is enter from Main Launcher.
When i press any one of the buttons, the Intent Action is changed.
My question:
The changed Intent action reflects some changed view in the ViewPager and does_not spawn a new Activity as such, only the view is updated.
What approach should i take to get this task?
Can i start the currentActivity using startActivity() and different Intent actions on button click?
or is there any other efficient way in android to do this?
[No need code, just explanation of logic / method would suffice]
Thanks in advance
If you are saying that you are trying to use startActivity to bring up the same activity again, and its not working, it could be because you set something like singleTop in your Android manifest.
If you are asking whether or not you should use an intent to change the state of your Activity, then the answer is "it depends". If the user would expect the back button to return your app to its previous state (instead of going back to the home screen), then it might be a good choice for you. If that is the case, however, I would ask why not just make 2 different Activities? Otherwise, just do as Dan S suggested and update the state of your Activity as the user interacts with it.
You can always use the onNewIntent() hook. Do something like this in your activity:
protected void onNewIntent(Intent intent){
//change your activity based on the new intent
}
Make sure to set the activity to singleTop in your manifest. Now whenever startActivity is called the onNewIntent() will be executed. Also, note that per the documentation:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.
I have successfully changed the transitions between activities using overridePendingTransition().
Unfortunately when I am on a TabActivity and use activities inside each tab. When one of those activities inside the content of the tab, starts another activity, the overridePendingTransition() seems to not work.
I basically have a TabActivity, inside it resides an activity with a ListView. What I'm doing is when the item is clicked, I launch the item details' activity.
This new activity's transition animation is not being overridden with the overridePendingTransition()
I basically do this:
private Activity owner;
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent programActivity = new Intent().setClass(view.getContext(), ProgramActivity.class);
Program program = (Program) parent.getItemAtPosition(position);
programActivity.putExtra("programID", program.getId());
owner.startActivity(programActivity);
owner.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
So, I believe that the pending transition is overridden after I'm trying to override them.
Is there a different place I should do that?
Am I doing some other stupid mistake?
Thanks !
I had a similar problem with calling overridePendingTransition() from within a Fragment, because Fragments do not have overridePendingTransition() method defined.
I solved it using:
getActivity().overridePendingTransition(R.anim.enterAnim, R.anim.exitAnim);
Hope it helps. The Fragment was within a TabHost.
I found out that the problem was because my view was a sub-activity inside a tab.
To correctly override the transitions I've overridden the onPause method on the TabActivity and it now works as expected.
Note: You still have to use the overridePendingTransition() on the listener for your items if your activity is NOT within a tab.
There are 4 Tabs in a TabHost, let them be A, B, C, and D. Now each one is just an index page and clicking on any of them shows a different activity.
The problem is that I need to start another activity when the user selects something from the content displayed in the tab. The other activity should also be displayed in the parent tab itself. Is it possible? Or will I have to try something else?
Try this, found this solution in android cookbook,
http://androidcookbook.com/Recipe.seam;jsessionid=5424397F3130CE7769FF47DD67742911?recipeId=1693&recipeFrom=ViewTOC
Can't you change the contentView of your tab instead of starting a new Activity ?
Maybe I'm wrong but I think also that starting an activity in a tab isn't possible because the TabView is hosted in a activity and not the opposite (Tabview don't host an activity per Tab).
I think the common consensus is that it is best not to use individual Activities as tab content due to these limitations. See these questions and answers for pointers to alternatives:
Android: Why shouldn't I use activities inside tabs?
Android - Tabs, MapView, activities within tabs
To summarize the link that Rukmal Dias provided. Here's what you do:
Change your current Activity (that's in a tab) to derive from ActivityGroup
Create a new intent for the Activity you want to switch to
Copy/Paste and call this function in your current activity where "id" is the "android:id" for the layout of the new activity you want to switch to
public void replaceContentView(String id, Intent newIntent){
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
this.setContentView(view);}
Here's an example of how I make the call to switch views from my current Tabbed Activity:
public void switchToNextActivity(View view)
{
Intent myIntent = new Intent(getApplicationContext(), MyNextActivity.class);
replaceContentView("next_activity", myIntent);
}
It looses the view hierarchy. When you press the back button, in my case, the app closes.