如何制作返回功能并设置最后一个contentView

发布于 2025-01-05 03:27:50 字数 689 浏览 0 评论 0原文

我的应用程序中有很多视图,现在的问题是如何从一个视图返回到另一个视图。

我可以通过在每个视图中设置后退按钮来做到这一点,但我想使用 android 后退硬按钮。

我尝试过这样的事情:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{       
  if(keyCode==KeyEvent.KEYCODE_BACK)
          {
      finish();
          }
return false;
}

但问题是这将关闭我的应用程序。 您能否指导我找到正确的解决方案,例如记住设置的最后一个视图,然后返回到此视图或类似的内容。

这是我用来更改视图的代码(这是我的主要活动中的一个方法):

public void CheckIfUserIsLogedIn ()
{
    int userPresence = localDatabase.CheckUserPresence();
    if (userPresence == 0) 
    {
        setContentView(R.layout.main);
    }
    else
    {
        setContentView(R.layout.userlogedon);
    }
}  

谢谢。

I hawe many view's in my application and now the problem is how to go back from one view to another.

What I could do it by set back Buttons in every view but i would like to use the android back hard button.

I have tried something like this:

public boolean onKeyDown(int keyCode, KeyEvent event) 
{       
  if(keyCode==KeyEvent.KEYCODE_BACK)
          {
      finish();
          }
return false;
}

But the problem is that this will close my application.
Could you please guide me for a proper solution, for example to memorize the last view was set and then to come back to this view or something like this.

Here is the code with which I am changing the view (it's a method in my main activity):

public void CheckIfUserIsLogedIn ()
{
    int userPresence = localDatabase.CheckUserPresence();
    if (userPresence == 0) 
    {
        setContentView(R.layout.main);
    }
    else
    {
        setContentView(R.layout.userlogedon);
    }
}  

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅此而已 2025-01-12 03:27:50

看!

你这样做是错误的。

Activity 类应该只包含内容 View。 (因为这是推荐的方式并且易于使用和实施)。

如果您想转到下一个View,请将其显示在另一个单独的Activity 下。

当您完成后,您将自动重定向到上一个Activity

(并且您不需要记住上一个视图:))

请参阅此处,了解如何使用 Activity 堆栈。

Look!

You are doing this wrong way..

An Activity class should only have on content View. (because it is recommended way and easy to use and implement).

And if you want to go to next View, show it under another separate Activity.

when you will finish it, you will be automatically redirected to previous Activity.

(and you don't need to memorize the Previous View :) )

See here, how to work with Activity Stacks.

平生欢 2025-01-12 03:27:50

我不确定是否正确理解你的问题,因为 Android 会自动为你完成所有这些工作。一旦打开一个视图,当您切换到另一个视图时,它就会暂停(在屏幕上但没有焦点)或停止(没有焦点)

http://developer.android.com/reference/android/app/Activity.html

如果当前视图(activity)已经被上一个视图(activity)启动,按后退按钮将使您“关闭”当前视图并且自动返回到上一个。

现在有两件事:

  1. 也许您只是通过显示打开或关闭组件来打开同一活动中的所有视图,这是一种不好的做法,并且 Android 不推荐。您应该做的是 1 次查看 = 1 次活动。

  2. 您的想法就像“iPhone/iPad”,您必须在“视图”中实现后退按钮。在 Android 中你不需要这样做。此时将“完成”命令放入代码中似乎会关闭应用程序,这让我认为您已经按照第 1 点中的说明进行了编程。

希望它有帮助

编辑

要开始一项新活动,请执行以下操作 你把它

startActivity(new Intent(this, MyOtherActivity.class));

放在你想要加载新视图(活动)的代码中

现在,如果你想在活动之间传输一些信息,你必须做这样的事情:

Intent myIntent; //intent declaration

int aNumber = 10; // info to send to other activity
String aString = "abcd"; // info to send to other activity

// link Intent to the other activity
myIntent = new Intent(view.getContext(), MyOtherActivity.class) 

//put the extra info
myIntent.putExtra("myNumber", aNumber);
myIntent.putExtra("myString", aString);

//start the new view/activity
startActivity(myIntent);

在新打开的活动中,你检索这样的信息(在通常在创建时)

int aNumber;
String aString;

@Override
public void onCreate(Bundle savedInstanceState) { 
    aNumber= getIntent().getExtras().getInt("myNumber");
    aString= getIntent().getExtras().getString("myString"); 
}

I am not sure to understand your problem correctly because Android do all that for you automatically. Once a view is opened when you switch to another view it is paused (on screen but has not focus) or stopped (has no focus)

http://developer.android.com/reference/android/app/Activity.html

If the current view (activity) has been launched by the previous view (activity), pressing the back button will make you "close" the current view and go back to the previous one automatically.

Now two things :

  1. Perhaps your are simply opening all views wihtin the same activity by showing on or off components which is a bad way of doing and is not recommended by android. What you should do is 1 view = 1 activity.

  2. You are thinking like "iPhone/iPad" where you have to implements back buttons in the "views". In android you don't need to do so. Putting the "finish" command in your code at that point seem to close the application which make me think you have programmed as explained in point 1.

Hope it helps

EDIT:

To start a new activity do it like this

startActivity(new Intent(this, MyOtherActivity.class));

you put this in your code where you want to load the new view (activity)

Now if you want to transfer some information between activities you must do something like this :

Intent myIntent; //intent declaration

int aNumber = 10; // info to send to other activity
String aString = "abcd"; // info to send to other activity

// link Intent to the other activity
myIntent = new Intent(view.getContext(), MyOtherActivity.class) 

//put the extra info
myIntent.putExtra("myNumber", aNumber);
myIntent.putExtra("myString", aString);

//start the new view/activity
startActivity(myIntent);

and in the new opened activity you retrieve the infos like this (in the oncreate usually)

int aNumber;
String aString;

@Override
public void onCreate(Bundle savedInstanceState) { 
    aNumber= getIntent().getExtras().getInt("myNumber");
    aString= getIntent().getExtras().getString("myString"); 
}
南城追梦 2025-01-12 03:27:50

实际上我不确定是否完全理解,但是..

拿一张地图或共享首选项,然后在后退按钮上设置最后一个“在地图上查看”或“共享首选项”。

在调用或开始活动时获取已存储的数据。

这会对你有帮助。

Actually i m not sure that understand exactly but..

take a map or a shared preference and at the back button set last View on map or Shared preference .

At the calling or at start activity fetch the data which have stored you.

this will helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文