-1

im new to android dev.
I start by follow some tutorial and i make a simple app.
I'm confused to the way start other activity.
I have 3 activity login, main, temp
when i at main activity i want to start temp activity by the code below:

@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;
} 

and i did the same in login activity but not working:

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();
}

instead i have to do this:

@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();
}  

and why requestCode == requestCode i couldn't find function setRequestCode like setResultCode
last question: should I use fragment instead of activity to share my NavigationBar because now i have to include navigationBar layout to tempActivity layout, and same java code in class

Felix
  • 571
  • 14
  • 34

2 Answers2

1

Firstly No need to use ProgressDialog for starting any activity it doesn't take too much of time, ProgressDialog is used for long running operations like server call ,upload/download image etc

Start Activity Simple

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

Thats it nothing more

Now you want to pass data to activity than

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

onActivityResult() this method is used when you want to get some data back from the activity you lunching

For this look at this post -https://stackoverflow.com/a/10407371/4741746

why requestCode == requestCode i couldn't find function setRequestCode like setResultCode

onActivityResult() method is only called in case of startActivityForResult() and you are calling startActivity ,calls like startActivityForResult (intent, 100); here 100 is requestCode and result code is

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

here Activity.RESULT_CANCELED is your resultCode

Suggestion is call activity like

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

should I use fragment instead of activity to share my NavigationBar

Yes you can just put this layout in your xml

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

and to add different fragment

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();                       
}

call this method in switch

switch (id){
        case R.id.item1:
            addYourFragment();
            break;
        case R.id.item2:           
            addYourFragmentTwo()
            break;
        case R.id.item3:
            addYourFragmentThree()
            break;
    }
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
0

you can help from these codes:

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;
                }
            }
        });
    }
}


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

  • i want to asked about why in from `login activity` have to do in difference way to start `main activity` – Felix May 29 '17 at 04:15
  • @HoàngĐăng, please accept my answer. please or upvote me –  May 29 '17 at 05:26