Reputation: 189
I need help saving my scrollview's position when resuming the activity. I have a long scrollview containing some buttons. When I click on those buttons, new activities are opened. When I resume the scrollview's activity, the scrollview starts from the top and the user has to scroll again. This is pretty annoying.
How can i save the scrollview position?
I found this, but the line: Util.Log("X:" + scrollX + " Y:" + scrollY);
doesn't work:
// two static variable,
public static int scrollX = 0;
public static int scrollY = -1;
//update & save their value on onPause & onResume.
@Override
protected void onPause()
{
super.onPause();
scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();
}
@Override
protected void onResume()
{
Util.Log("X:" + scrollX + " Y:" + scrollY);
super.onResume();
//this is important. scrollTo doesn't work in main thread.
scrollView.post(new Runnable()
{
@Override
public void run()
{
scrollView.scrollTo(scrollX, scrollY);
}
});
}
Thank you for your help! :D
EDIT:
If i remove the line, I'll get this error:
01-01 17:29:11.023: E/AndroidRuntime(1967): FATAL EXCEPTION: main
01-01 17:29:11.023: E/AndroidRuntime(1967): Process: com.example.appquiz, PID: 1967
01-01 17:29:11.023: E/AndroidRuntime(1967): java.lang.RuntimeException: Unable to resume activity {com.example.appquiz/com.example.appquiz.Categories}: java.lang.NullPointerException
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2808)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2837)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2270)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.os.Looper.loop(Looper.java:136)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-01 17:29:11.023: E/AndroidRuntime(1967): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 17:29:11.023: E/AndroidRuntime(1967): at java.lang.reflect.Method.invoke(Method.java:515)
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-01 17:29:11.023: E/AndroidRuntime(1967): at dalvik.system.NativeStart.main(Native Method)
01-01 17:29:11.023: E/AndroidRuntime(1967): Caused by: java.lang.NullPointerException
01-01 17:29:11.023: E/AndroidRuntime(1967): at com.example.appquiz.Categories.onResume(Categories.java:82)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.Activity.performResume(Activity.java:5319)
01-01 17:29:11.023: E/AndroidRuntime(1967): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2798)
01-01 17:29:11.023: E/AndroidRuntime(1967): ... 12 more
My entire code:
public static int scrollX = 0;
public static int scrollY = -1;
ScrollView scrollView = (ScrollView) findViewById(R.id.Scrollview);
//blabla
onCreate //...
@Override
protected void onPause() {
// TODO Auto-generated method stub
scrollX = scrollView.getScrollX();
scrollY = scrollView.getScrollY();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
scrollView.post(new Runnable(){
@Override
public void run(){
scrollView.scrollTo(scrollX, scrollY);}});}
Upvotes: 0
Views: 1516
Reputation: 500
add this code to the end of your inflateView method and please make sure that R.id.Scrollview is the right Id
scrollView = (ScrollView) findViewById(R.id.Scrollview);
By the way, you should not use post method to do this. The best way to scroll the view is to do the following:
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
scrollView.scrollTo(0, 0);
}
});
If that does not help please tell us what line is this:(com.example.appquiz.Categories.onResume(Categories.java:82))
Upvotes: 3
Reputation: 1325
Util.Log("X:" + scrollX + " Y:" + scrollY); remove this code dude this is just logging code and nothing to do with functionality
Upvotes: 0