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.
Related
I have two Activities A and B.
Activity A has a Tablayout with some Tabs. When I navigate from A to B I use this:
Intent intent = new Intent(A, B.class);
A.startActivity(intent);
When I now navigate back from B to A I have a question:
1) When using Android's back button, the selected tab / scrolling position from A was remembered
2) When using an Intent or NavUtils.navigateUpFromSameTask(this); then the selected tab and scroll Position is NOT remembered but set to initial value
Can someone explain me what is going on here?
1) when navigation from activity A to B, the android system does not destroy activity A, but takes it to the back stack and adds B to the foreground. thats why when you press the back button or call onBackPressed() from the java code activity B is destroyed and A is set to the foreground. here is an example from the docs : Understand Tasks and Back stack
2) when using an intent/navigateUpFromSameTask activity A is recreated and set to the foreground and B is set to the background, it's like adding another activity A to the stack so it will be A,B,A but if you press the back btn then you will be back to B and then A.
if you want to keep the scroll position and other data in activity A you call the onBackPressed in B or use the onSaveInstanceState to save the data and use it in the onCreate .
here is an example of saved instance:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
savedInstanceState.putString("VariableName", variableData);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.penguin_main);
if(savedInstanceState!=null){
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
bookData = (String) savedInstanceState.getSerializable("VariableName");
}
}
You can set the current scroll position and tab position in activity A's on overriding onSaveInstance(Bundle savedInstanceState) method. When return to activity you can get onRestoreInstanceState(Bundle savedInstanceState) to restore it.
Hope it helps :)
Because NavUtils.navigateUpFromSameTask() just calls startActivity() and if android:launchMode="standard" the activity will be instantiated and created again and that is why can not remember the previous selected tab. To solve this issue you can override onNavigateUp() and inside that setCurrentItem(index) the index of tab you want to be displayed.
#Override
public boolean onNavigateUp() {
myViewPager.setCurrentItem(position, true);
return true;
}
Edit
You can use another solution to solve the problem by setting android:launchMode="singleTop" on activity but this solution may not applicable in all the application.
When you start an Activity, the first page will be opened! But when the back button is pressed, it navigates between the saved state of the activityTo simulate the back button pressed, you can try this:
#Override
public void onBackPressed() {
finish(); //your choice, thought not needed as super.onBackPressed(); is called if nothing is assigned here
}
or on toolbar back button clicked click:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
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 am having Activity A and Two Fragment called Fragment A and Fragment B.
In Fragment B List View is implemented. On Clicking of any list item the new activity get instantiated (Activity B). The Problem with the scenario 2
if the user pres the home button and again resume the activity then activity 2 is getting resumed.After resuming activity if the user press the back button then activity is getting into pause stage instead of returning back into to the parent fragment(Fragment 2).
Manifest for Activity 2
<activity
android:name=".activity.Activity2"
android:label="#string/label1"
android:parentActivityName=".activity.Activity1"
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.activity.Activity1" />
</activity>
Let me know what's causing this behavior. How can i retain fragment B when Activity B move into pause state.? or any other solution?
You could override the onBackPressed of your second activity, so that you always return to the first activity with fragment B shown.
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("some tag", "some text");
startActivity(intent);
}
And in your first activity do something lke that:
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
if(intent.getStringExtra("some tag").equals("some text"))
{
methodToDisplayFragmentB();
}
}
Hope this helps.
I have an Activity with a ListView (ItemsActivity) which its contents come from a JSON API. When I press in one item in the ListView it loads another Activity with the details information (DetailActivity) . The problem is, when I press the Back Button, ItemsActivity reload the ListView again.
I don't know where I can find more information about this. I came from iOS where the previous screen is not reload every time.
I want to keep the ListView data between activities. I tested to call loadListItems() method from onResume() but same result.
Here is and brief sample of my code. Any help and suggestions will be really appreciated.
/* ItemsActivity.java */
public class ItemsActivity extends AppCompatActivity {
private ListView listItemView;
private Movie[] movies;
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
listItemView = (ListView) findViewById(R.id.listItemView);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
loadListItems();
}
private void loadListItems(){
// Http call
...
// Iterate JSON and saving to movies array
...
progressBar.setVisibility(INVISIBLE);
ListAdapter adapter = new ListAdapter(ItemsActivity.this, movies);
listItemView.setAdapter(adapter);
}
/* Adapter Class */
#Override
public void onClick(View v) {
Intent i = new Intent(this, DetailActivity.class);
i.putExtra("item_id", 1);
startActivity(i);
}
}
I just tested with a new Project with 2 Activities, ActivityOne and ActivityTwo. ActivityOne have a button, when a pressed its load ActivityTwo and when I press the back button, in ActivityOne the method onCreate() is called again.
You mentioned that "I tested to call loadListItems() method from onResume() but same result." This onResume() code is culprit.
OnResume() will get called whenever your activity gets focus again. If you are calling loadListItems() in onResume() then it will get called everytime you come back from your second activity to this activity.
http://developer.android.com/reference/android/app/Activity.html#onResume()
A solution is to use StartActivityForResult and not StartActivity. Then in the second activity, override the onBackPressed method and finish() the second activity within this method. Override the onActivityResult method in your first activity and this activity will be called and not the onCreate method and the ListView will not be reloaded.
The problem was only when I pressed the back button from Action Bar and I fix it with onOptionsItemSelected method.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
I have two Activities: Activity A and Activity B. Activity A consists of a custom ListView and a custom ListView Adapter. When chosing an item of the ListView Activity B appears and displays the user relevant information. When pressing the back/return button Activity B calls finish().
I do not intend to create as many activites as items the ListView has. I want Activity B to be able to change it's layout id. So far my application crashes on every attempt I make to turn the setContentView method variable.
relevant code from Activity A :
// code
if (position == 0 && imaginaryInt == 3) // item position in the ListView
{
ActivityB b = new ActivityB();
b.setLayoutID(R.layout.main_c); // this line causes the crash
Intent intent = new Intent(view.getContext(),b.getClass());
startActivityForResult(intent, 0);
if (position == 1 && imaginaryInt == 3 ) {...}
Acivity B :
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_b);
}
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK,intent);
super.onBackPressed();
}
}
Overriding setContentView() in ActivityB and changing params to R.layout.main_c did work!
Unfortunately I ran out of ideas how to manage to change the the layout of ActivityB from ActivityA.
Any help is highly appreciated.
EDIT: ---SOLUTION---
To be able to change the layout od ActivityB from ActivityA I gave each ListView click a static unique id. In ActivityB the layout is simply changed with the setContentView method and if the row with the unqiue id was clicked.
You can pass the information you want through the Intent used to start the Activity. For example:
if (position == 0 && imaginaryInt == 3)
{
Intent intent = new Intent(view.getContext(),ActivityB.class);
intent.putExtra("layout_id", R.layout.main_c); // pass the id as an int extra
startActivityForResult(intent, 0);
}
And then, in your ActivityB class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layoutId = getIntent().getIntExtra("layout_id", R.layout.default_value);
setContentView(layoutId);
}
You cannot operate on layout of ActivityB from ActivityA.
on this line:
b.setLayoutID(R.layout.main_c);
b is NOT EXISTENT at the time you are trying to call. It will exist only after startActivity.
And even then you, as I remember, cannot do that from activity A - activity B is not synchronized with A.
Use other means: for example create service to signal ActivityB to change layout.