我正在开发一个由2个活动组成的android Wallpaper应用,主要活动在ListView中显示来自Internet的图像,第二个活动显示该图像的预览。 我的问题是,当我在预览活动中按“后退”按钮返回到主活动时,主活动从头开始显示图像,而从最后一次单击开始我要显示图像。
主要活动中onCreate()方法中的以下代码:
// Find a reference to the {@link ListView} in the layout
ListView gameListView = findViewById(R.id.list);
mEmptyStateTextView = findViewById(R.id.empty_view);
gameListView.setEmptyView(mEmptyStateTextView);
// Create a new adapter that takes an empty list of games as input
mAdapter = new GamesAdapter(this, new ArrayList<Games>());
// Set the adapter on the {@link ListView}
// so the list can be populated in the user interface
gameListView.setAdapter(mAdapter);
// Set an item click listener on the ListView, which sends an intent to a web browser
// to open a website with more information about the selected game.
gameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Find the current game that was clicked on
Games currentGame = mAdapter.getItem(position);
// Convert the String URL into a URI object (to pass into the Intent constructor)
Uri gameUri = Uri.parse(currentGame.getUrl());
String name = currentGame.getName();
// Create a new intent to view the game URI
Intent i = new Intent(GamesActivity.this, PreviewActivity.class);
i.setData(gameUri);
i.putExtra("name", name);
startActivity(i);
}
});
预览活动中onCreate()方法中的以下代码:
final Uri i = getIntent().getData();
String profile = getIntent().getStringExtra("name");
photographer.setText(profile);
Picasso.with(this).load(i).into(img);
saveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
downloadFile(i);
}
});
答案 0 :(得分:1)
我认为您正在寻找的是方法onSaveInstanceState(Bundle outState)
,您可以在其中将值保存在outState中,并在onCreate()
的{{1}}中获取它们
here是一个很好的用法示例。
答案 1 :(得分:0)
我认为您想保留滚动距离。为此,您可以使用Bundle
对象进行保存。但是您也可以清除当前活动,然后将获得相同的结果。
将其添加到onCreate()
方法之外(这是java):
@Override
public void onBackPressed() {
this.finish(); //important
}
finish()
从后堆栈清除活动。