Activity Reload every time

I have an Activity with a ListView (ItemsActivity) whose contents come from the JSON API. When I click one item in a ListView, it loads another action with details information (DetailActivity). The problem is that when I click the back button, the ItemsActivity reloads the ListView again.

I do not know where I can find additional information about this. I came from iOS where the previous screen does not restart every time.

I want to save ListView data between actions. I tested the call to the loadListItems () method from onResume (), but the same result.

Here is a short sample of my code. Any help and suggestions would 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);
  }
}

, ActivityOne ActivityTwo. ActivityOne , ActivityTwo "", ActivityOne onCreate().

+4
3

, " loadListItems() onResume(), ". onResume() .

OnResume() , . loadListItems() onResume(), , . http://developer.android.com/reference/android/app/Activity.html#onResume()

0

, StartActivityForResult, StartActivity. onBackPressed () . onActivityResult , , onCreate, ListView .

0

, "" , onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }

    return super.onOptionsItemSelected(item);
}
0

Source: https://habr.com/ru/post/1612518/


All Articles