M
M
Megabober2020-05-25 18:56:02
Android
Megabober, 2020-05-25 18:56:02

How to use the back button to return to the same position in the previous activity?

Hello, I'm a beginner. Problem in the following:

There is main_activity.xml in which in scrollview there is a large number of LinearLayout. Below is an example (I left only 2 here, in fact there are about 100 of them). Clicking on these LinearLayout opens new activities. And when you press the BACK button, it returns to the previous activity to the very top.

How to make it so that when the Back button is pressed, it returns to the position from where the click was, and not to the very top.

And it turns out that I scrolled, clicked, read, clicked back and ended up at the very top, you need to re-scroll to the place where the click was from. Codes below:

This is markup. Clicks go to android:id="@+id/kart1" (up to kart100 in total)

<?xml version="1.0" encoding="utf-8"?>
xmlns:app="schemas.android.com/apk/res-auto "
xmlns:tools=" schemas.android.com/tools "
android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/kont1 "
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

android:layout_width="match_parent"
android:layout_height="match_parent">

android:id="@+id/kont2"
android :layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical" >

android:id="@+id/kart1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/kont_res1"
android:orientation="horizontal" >

android:layout_width="92dp"
android:layout_height="92dp"
android:layout_margin="10dp"
android:orientation="horizontal">

android:id="@+id/image_kart1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/kart_fon"
android:scaleType="centerCrop"
android:src="@drawable/kart1_avran" />


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="18dp"
android:layout_gravity="center"
android:orientation="horizontal">

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:textSize="16sp"
android:lineHeight="21sp"
android:text="@string/rast1"
android :textColor="@color/black" />



android:id="@+id/kart2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="@drawable/kont_res2"
android:orientation="horizontal">

android:layout_width="92dp"
android:layout_height="92dp"
android:layout_margin="10dp"
android:orientation="horizontal">

android:id ="@+id/image_kart2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/kart_fon"
android:scaleType="centerCrop"
android:src="@drawable/kart2_adonis" />


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="18dp"
android:layout_gravity="center"
android:orientation="horizontal">

android: layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:lineHeight="21sp"
android:textColor="@color/black"
android:fontFamily="sans-serif-medium"
android:text="@string/rast2"/>








This is the click on which we go to the new activity (there are also 100 such transitions)

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.kart1);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity.this, Rast1.class);
startActivity(intent);
finish();

}catch (Exception e) {
}
}
});
}

This is a system button back to the previous

activity @Override
public void onBackPressed(){
try {
Intent intent = new Intent(Rast1.this, MainActivity.class);
startActivity(intent);
finish();
}catch (Exception e) {
}
}

And I added this to the back button at the top left of the screen

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
Intent intent = new Intent(Rast1.this, MainActivity.class);
startActivity(intent);
finish();
}
return super.onOptionsItemSelected(item);
}

If possible, write in more detail, or if you can, insert it into my code, otherwise I'm a beginner))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Parfenov, 2020-05-25
@pontifex024

Just don't override onBackPressed at all. By backing, and so there will be a return back to the previous activity. And in this code, a new activity opens on the back, which looks like the first one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question