English 中文(简体)
Android - 一些代码在电话到另一个活动后执行
原标题:Android - some code executes after the phone went to a different Activity
  • 时间:2012-05-24 01:11:55
  •  标签:
  • android

我这里有个奇怪的场景

我有这个密码:

// For checking if the person is logged in.
first_time_check();

setContentView(R.layout.main);

// ...next lines of code

如果用户首次登录, 则第一次_ time_ check () 函数检查。 如果用户的用户_ id 不在共享属性中, 我将它们重新定位为登录 :

public void first_time_check()
{   
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( ProblemioActivity.this);

    String user_id = prefs.getString( "user_id", null ); // First arg is name and second is if not found.
    String first_time_cookie = prefs.getString( "first_time_cookie" , null );               

    // About setting cookie.  Check for first time cookie
    if ( first_time_cookie == null )
    {
        // This user is a first-time visitor, 1) Create a cookie for them
        first_time_cookie = "1";

        // 2) Set it in the application session.
        prefs.edit().putString("first_time_cookie", first_time_cookie ).commit(); 

        // Make a remote call to the database to increment downloads number
        // AND send me an email that it was a new user.                 
    }        
    else
    {
        // If not first time, get their id.
        // If user_id is empty, make them an account.  
        // If id not empty, call the update login date and be done.         

        if ( user_id == null )
        {
            // User id is null so they must have logged out.                                
            Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
            ProblemioActivity.this.startActivity(myIntent);
        }
        else
        {               
            // Make a remote call to the database to increment downloads number                                 
        }
    }

return;
}       

所以在代码执行后

            Intent myIntent = new Intent(ProblemioActivity.this, LoginActivity.class);
            ProblemioActivity.this.startActivity(myIntent);

它仍然在调用此函数的原始代码下执行。

知道这怎么可能发生吗?

谢谢!

最佳回答

本文摘自Dev 指南

Shutting Down an Activity
You can shut down an activity by calling its finish() method.
You can also shut down a separate activity that you previously
started by calling finishActivity().

Note: In most cases, you should not explicitly finish an activity
using these methods. As discussed in the following section about the
activity lifecycle, the Android system manages the life of an
activity for you, so you do not need to finish your own activities.
Calling these methods could adversely affect the expected user
experience and should only be used when you absolutely do not want
the user to return to this instance of the activity.

在此拨打活动结束() 似乎是合适的, 因为您不想让用户返回到此活动 。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签