Android以正确的方式启动活动

时间:2017-05-29 03:13:42

标签: android android-activity

我对android dev的新手 我从一些教程开始,然后制作一个简单的应用程序 我对开始其他activity的方式感到困惑 我有3个活动loginmaintemp
当我在main activity时,我希望通过以下代码启动temp activity

@Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    Intent nextIntent;
    switch (id){
        case R.id.item1:
            nextIntent = new Intent(MainActivity.this, TempActivity.class);
            startActivity(nextIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
            break;
        case R.id.item2:
            nextIntent = new Intent(MainActivity.this, TempActivity.class);
            startActivity(nextIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
            break;
        case R.id.item3:
            nextIntent = new Intent(MainActivity.this, TempActivity.class);
            startActivity(nextIntent);
            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
            break;
    }

    drawer.closeDrawer(GravityCompat.START);
    return true;
} 

我在login activity做了同样的事情,但没有工作:

private void login() {
    Log.d(TAG, "Login");

    _loginButton.setEnabled(false);

    //show spinner
    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this,
            R.style.AppTheme_Dark_Dialog);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();

    // TODO: Implement your own authentication logic here.

    new android.os.Handler().postDelayed(
            new Runnable() {
                public void run() {
                    // On complete call either onLoginSuccess or onLoginFailed
                    onLoginSuccess();
                    // onLoginFailed();
                    progressDialog.dismiss();
                }
            }, 3000);
}
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == requestCode) {
        if (resultCode == RESULT_OK) {

            // TODO: Implement successful signup logic here
            // By default we just finish the Activity and log them in automatically
            startActivity(new Intent(this, MainActivity.class));
            this.finish();
        }
    }
}

public void onLoginSuccess() {
    //do nothing
    finish();
}

相反,我必须这样做:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == requestCode) {
        if (resultCode == RESULT_OK) {

            // TODO: Implement successful signup logic here
            // By default we just finish the Activity and log them in automatically
            this.finish();
        }
    }
}

public void onLoginSuccess() {
    startActivity(new Intent(this, MainActivity.class));
    setResult(RESULT_OK);
    finish();
}  

以及为什么 requestCode == requestCode我无法找到setRequestCode之类的功能setResultCode 上一个问题:我应该使用fragment而不是activity来分享我的NavigationBar,因为现在我必须将navigationBar布局包含到tempActivity布局,以及类

中的java code

2 个答案:

答案 0 :(得分:1)

首先,无需使用ProgressDialog来启动任何不花费太多时间的活动,ProgressDialog用于长时间运行的操作,如服务器调用,上传/下载图像等< / p>

启动活动简单

Intent intent = new Intent(YourCurrentActivity.this, LoginActivity.class);
startActivity(intent);

仅此而已

现在您要将数据传递给活动而不是

Intent intent = new Intent(YourCurrentActivity.this, LoginActivity.class);
intent.putExtra("INTENT_PARAM", YourValue);
startActivity(intent);

onActivityResult()当您想要从您正在进行的活动中获取一些数据时使用此方法

看看这篇文章的内容 - https://stackoverflow.com/a/10407371/4741746

  

为什么requestCode == requestCode我找不到函数setRequestCode   比如setResultCode

onActivityResult()方法仅在startActivityForResult()的情况下调用,并且您正在调用startActivity,此处调用startActivityForResult (intent, 100);这里的调用100是requestCode,结果代码是

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish(); 

这里Activity.RESULT_CANCELED是你的resultCode

建议是呼叫活动,如

Intent intent = new Intent(YourCurrentActivity.this, LoginActivity.class);
startActivity(intent);
  

我应该使用片段而不是活动来共享我的NavigationBar

是的,您可以将此布局放在xml中

<RelativeLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

并添加不同的片段

public void addYourFragment(){
                YourFragment myFragment = new YourFragment();
                FragmentManager fragmentManager = this.getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, myFragment, tagToUniqlyIdentifiedFramgent);
                fragmentTransaction.addToBackStack(tagToUniqlyIdentifiedFramgent);
                fragmentTransaction.commit();                       
}

在开关

中调用此方法
switch (id){
        case R.id.item1:
            addYourFragment();
            break;
        case R.id.item2:           
            addYourFragmentTwo()
            break;
        case R.id.item3:
            addYourFragmentThree()
            break;
    }

答案 1 :(得分:0)

您可以从这些代码中获得帮助:

public class ListActivityExample extends ListActivity {
    static final String[] ACTIVITY_CHOICES = new String[] {
        "Open Website Example",
        "Open Contacts",
        "Open Phone Dialer Example",
        "Search Google Example",
        "Start Voice Command"
    };
    final String searchTerms = "superman";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, ACTIVITY_CHOICES));
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setTextFilterEnabled(true);
        getListView().setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                switch(arg2) {
                case 0: //opens web browser and navigates to given website
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://www.android.com/")));
                    break;
                case 1: //opens contacts application to browse contacts
                    startActivity(new Intent(Intent.ACTION_VIEW,
                           Uri.parse("content://contacts/people/")));
                    break;
                case 2: //opens phone dialer and fills in the given number
                    startActivity(new Intent(Intent.ACTION_VIEW,
                           Uri.parse("tel:12125551212")));
                    break;
                case 3: //search Google for the string
                    Intent intent= new Intent(Intent.ACTION_WEB_SEARCH );
                    intent.putExtra(SearchManager.QUERY, searchTerms);
                    startActivity(intent);
                    break;
                case 4: //starts the voice command
                    startActivity(new
                                    Intent(Intent.ACTION_VOICE_COMMAND));
                    break;
                default: break;
                }
            }
        });
    }
}


http://www.informit.com/articles/article.aspx?p=1646053&seqNum=3

中的详细信息