0

我的情况如下所述。

活动 A 可以调用 B 或 D。

B 调用 C,在 C 中输入详细信息后,按返回,C 会将数据传回 B。从 B 中,如果我选择任一行,该行将带我到 D。

现在,我的问题是,从 D 开始,我想设置 if-else 语句以知道 B 中的哪个 StartActivityForResult 正在调用 D,以便我可以将各自的数据从 C 发送到 B 和 D。

       _____>B------>C
      /      |
A---->       |(B can call D and pass data from selected row from B to D.)
      \_____>D

代码活动 D:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.tab_content_setting);
        this.initViews();
        bluguard = BluGuardProtocol.getInstance();
    }

    private void initViews(){
        mainActivity = (BluGuardProto2Activity) this.getParent();
        toolbar = (ToolBar)mainActivity.findViewById(R.id.toolbar);
        etAccountName = (EditText)this.findViewById(R.id.etAccountName);
        etWanIp = (EditText)this.findViewById(R.id.etWanIp);
        etLocalIp = (EditText)this.findViewById(R.id.etLocalIp);
        etPort = (EditText)this.findViewById(R.id.etPort);
        etPassword = (EditText)this.findViewById(R.id.etPassword);
        switcher = (ViewSwitcher)this.findViewById(R.id.switcher);
        infoView = (RelativeLayout)this.findViewById(R.id.infoView);
        btnConnection = (Button)this.findViewById(R.id.btnConnection);

        btnConnection.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(bluguard.isLoggedInAndConnected()){
                    toolbar.disconnect();
                    btnConnection.setText(getResources().getString(R.string.connect));} else {
                    toolbar.connect();}}});

                // Assigns value
    String choice = getIntent().getStringExtra("activity");
    //Bundle extras = intent.getExtras();
    SharedPreferences  sp = PreferenceManager.getDefaultSharedPreferences(this);
    if(choice != null && choice.equals("activity2")) {
    //if(extras.contains("activity2")){
    etAccountName.setText(sp.getString("accountName2", ""));
    etWanIp.setText(sp.getString("wanIp2", ""));
    etLocalIp.setText(sp.getString("localIp2", ""));
    etPort.setText(sp.getString("port2", ""));
    etPassword.setText(sp.getString("password2", ""));}
    }

    @Override
    protected void onResume() {
        super.onResume();
        this.toolbar.setProtocolCallback(this);
        this.toolbar.setRightButtonDrawable(R.drawable.ic_btn_info);
        this.toolbar.setRightButtonOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                showInfoView();}});

        this.toolbar.setLeftButtonOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                if(bluguard.isLoggedInAndConnected()){
                    toolbar.disconnect();} else {
                    toolbar.connect();}}});

        if(bluguard.isLoggedInAndConnected()){
            btnConnection.setText(getResources().getString(R.string.disconnect));
        } else {
            btnConnection.setText(getResources().getString(R.string.connect));}}

    private void showInfoView(){
        mainActivity.hideTabs();

        this.toolbar.setRightButtonDrawable(0);
        this.toolbar.setRightButtonOnClickListener(null);
        this.toolbar.setLeftButtonDrawable(R.drawable.ic_btn_back);
        this.toolbar.setLeftButtonOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                showSettingsView();
            }
        });
        this.toolbar.setText(getResources().getString(R.string.about));

        switcher.showNext();
    }

    private void showSettingsView(){
        this.toolbar.initConnectionUI();
        this.toolbar.setRightButtonDrawable(R.drawable.ic_btn_info);
        this.toolbar.setRightButtonOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View arg0) {
                showInfoView();
            }
        });
        mainActivity.showTabs();
        switcher.showPrevious();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        Intent i = new Intent(SettingActivity.this, WelcomeActivity.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }

    @Override
    public void onLoginCompleted(boolean succeeded, int code) {
        if(succeeded){
            btnConnection.setText(getResources().getString(R.string.disconnect));
        }
    }

    @Override
    public void onToggleAlarmCompleted(boolean succeeded, int code) {
        // Do nothing
    }

    @Override
    public void onToggleHACompleted(boolean succeeded, int code) {
        // Do nothing
    }

    @Override
    public void onStatusPollCompleted(AlarmStatus status) {
        // Do nothing}

    @Override
    public void onDisconnected() {
        btnConnection.setText(getResources().getString(R.string.connect));}

    @Override
    public void onRequestTimedOut(TimeOutType type) {
        // Do nothing}

    @Override
    public void onPreReconnect() {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);}}

代码活动 B:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_main);
        this.initViews();

        //Set onClick on Button Add
        Button btnAdd = (Button)findViewById(R.id.btnAdd);        
        btnAdd.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (tableRow1.getVisibility() == (View.GONE)){
                    tableRow1.setVisibility(View.VISIBLE);
                    border1.setVisibility(View.VISIBLE);}
                else if (tableRow2.getVisibility() == (View.GONE)){
                        tableRow2.setVisibility(View.VISIBLE);
                        border2.setVisibility(View.VISIBLE);}
                else if (tableRow3.getVisibility() == (View.GONE)){
                        tableRow3.setVisibility(View.VISIBLE);
                        border3.setVisibility(View.VISIBLE);}
                else if (tableRow4.getVisibility() == (View.GONE)){
                        tableRow4.setVisibility(View.VISIBLE);
                        border4.setVisibility(View.VISIBLE);}
                else if (tableRow5.getVisibility() == (View.GONE)){
                        tableRow5.setVisibility(View.VISIBLE);
                        border5.setVisibility(View.VISIBLE);}
                else if (tableRow6.getVisibility() == (View.GONE)){
                        tableRow6.setVisibility(View.VISIBLE);
                        border6.setVisibility(View.VISIBLE);}
                else if (tableRow7.getVisibility() == (View.GONE)){
                        tableRow7.setVisibility(View.VISIBLE);
                        border7.setVisibility(View.VISIBLE);}
                else if (tableRow8.getVisibility() == (View.GONE)){
                        tableRow8.setVisibility(View.VISIBLE);
                        border8.setVisibility(View.VISIBLE);}
                else if (tableRow9.getVisibility() == (View.GONE)){
                        tableRow9.setVisibility(View.VISIBLE);
                        border9.setVisibility(View.VISIBLE);}
                else if (tableRow10.getVisibility() == (View.GONE)){
                        tableRow10.setVisibility(View.VISIBLE);
                        border10.setVisibility(View.VISIBLE);}
                else {
                        final AlertDialog alertDialog = new AlertDialog.Builder(AccountSetup.this).create();
                        alertDialog.setTitle("Alert Message");
                        alertDialog.setMessage("You have reached the maximum number of users");
                        alertDialog.setIcon(R.drawable.icon_tick);
                        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                        alertDialog.show();
                    }   
        }});
    }

    private void initViews(){ 
        //Account 1  
        tableRow1  = (TableRow)findViewById(R.id.tableRow1);
        border1    = (View)findViewById(R.id.border1);
        iconNext1  = (Button)findViewById(R.id.iconNext1);
        tableRow1.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    tableRow1.setVisibility(View.GONE);
                    border1.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();}});
            alertDialog.show();
            return true;
            }});
        tableRow1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                /*tableRow1.setEnabled(true);
                tableRow1.setSelected(true);
                tableRow1.requestFocusFromTouch();*/
                Intent Intent1 = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                Intent1.putExtra("activity", "activity1");
                startActivityForResult(Intent1, 150);
                }
        });          
        iconNext1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent1 = new Intent(AccountSetup.this, AccountSettingActivity1.class);
            startActivityForResult(Intent1, 100);}});

        //Account 2
        tableRow2  = (TableRow)findViewById(R.id.tableRow2);  
        border2    = (View)findViewById(R.id.border2);
        iconNext2  = (Button)findViewById(R.id.iconNext2);
        tableRow2.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow2.setVisibility(View.GONE);
                border2.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});
        tableRow2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                    /*tableRow1.setEnabled(true);
                    tableRow1.setSelected(true);
                    tableRow1.requestFocusFromTouch();*/
                    Intent Intent2 = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                    Intent2.putExtra("activity", "activity2");
                    startActivityForResult(Intent2, 250);
                    }
            });
        iconNext2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent2 = new Intent(AccountSetup.this, AccountSettingActivity2.class);
            startActivityForResult(Intent2, 200);}});

        //Account 3
        tableRow3  = (TableRow)findViewById(R.id.tableRow3);  
        border3    = (View)findViewById(R.id.border3);
        iconNext3  = (Button)findViewById(R.id.iconNext3); 
        tableRow3.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow3.setVisibility(View.GONE);
                border3.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});   
        iconNext3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            Intent Intent3 = new Intent(AccountSetup.this, AccountSettingActivity3.class);
            startActivityForResult(Intent3, 300);}});

        //Account 4
        tableRow4  = (TableRow)findViewById(R.id.tableRow4);  
        border4    = (View)findViewById(R.id.border4);
        iconNext4  = (Button)findViewById(R.id.iconNext4); 
        tableRow4.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow4.setVisibility(View.GONE);
                border4.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});              
        iconNext4.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent4 = new Intent(AccountSetup.this, AccountSettingActivity4.class);
            startActivityForResult(Intent4, 400);}});

        //Account 5
        tableRow5  = (TableRow)findViewById(R.id.tableRow5);  
        border5    = (View)findViewById(R.id.border5);
        iconNext5  = (Button)findViewById(R.id.iconNext5); 
        tableRow5.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow5.setVisibility(View.GONE);
                border5.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});
        iconNext5.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent5 = new Intent(AccountSetup.this, AccountSettingActivity5.class);

            startActivityForResult(Intent5, 500);}});

        //Account 6
        tableRow6  = (TableRow)findViewById(R.id.tableRow6);  
        border6    = (View)findViewById(R.id.border6); 
        iconNext6  = (Button)findViewById(R.id.iconNext6); 
        tableRow6.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow6.setVisibility(View.GONE);
                border6.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext6.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent6 = new Intent(AccountSetup.this, AccountSettingActivity6.class);
            startActivityForResult(Intent6, 600);}});

        //Account 7
        tableRow7  = (TableRow)findViewById(R.id.tableRow7);  
        border7    = (View)findViewById(R.id.border7); 
        iconNext7  = (Button)findViewById(R.id.iconNext7);
        tableRow7.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow7.setVisibility(View.GONE);
                border7.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext7.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent7 = new Intent(AccountSetup.this, AccountSettingActivity7.class);
            startActivityForResult(Intent7, 700);}});

        //Account 8
        tableRow8  = (TableRow)findViewById(R.id.tableRow8);  
        border8    = (View)findViewById(R.id.border8);  
        iconNext8  = (Button)findViewById(R.id.iconNext8);
        tableRow8.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow8.setVisibility(View.GONE);
                border8.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});    
        iconNext8.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent8 = new Intent(AccountSetup.this, AccountSettingActivity8.class);
            startActivityForResult(Intent8, 800);}});

        //Account 9
        tableRow9  = (TableRow)findViewById(R.id.tableRow9);  
        border9    = (View)findViewById(R.id.border9);  
        iconNext9  = (Button)findViewById(R.id.iconNext9);
        tableRow9.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                tableRow9.setVisibility(View.GONE);
                border9.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();}});
            alertDialog.show();
            return true;}});     
        iconNext9.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent9 = new Intent(AccountSetup.this, AccountSettingActivity9.class);
            startActivityForResult(Intent9, 900);}});

        //Account 10
        tableRow10  = (TableRow)findViewById(R.id.tableRow10);  
        border10    = (View)findViewById(R.id.border10);
        iconNext10  = (Button)findViewById(R.id.iconNext10);
        tableRow10.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(AccountSetup.this);
            alertDialog.setTitle("User Account Deletion");
            alertDialog.setMessage("Are you sure to delete this user account?");
            alertDialog.setIcon(R.drawable.icon_alert);
            alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    tableRow10.setVisibility(View.GONE);
                    border10.setVisibility(View.GONE);}});
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();}});
            alertDialog.show();
            return true;}});      
        iconNext10.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent Intent10 = new Intent(AccountSetup.this, AccountSettingActivity10.class);
            startActivityForResult(Intent10, 1000);}});
    }

    public void onBackPressed() {
        Intent i = new Intent(AccountSetup.this, WelcomeActivity.class);
        startActivity(i);
        finish();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
       if (requestCode == 100) {
           data.getStringExtra("accountName1");  
           data.getStringExtra("wanIp1");
           data.getStringExtra("localIp1");
           data.getStringExtra("port1");
           data.getStringExtra("password1");
           if (resultCode == RESULT_OK) {      
               TextView user1 = (TextView)findViewById(R.id.user1);  
               user1.setText(sp.getString("accountName1", ""));}}

       else if ((requestCode == 150)&&(resultCode == RESULT_OK)) {
           data.getStringExtra("accountName1");  
           data.getStringExtra("wanIp1");
           data.getStringExtra("localIp1");
           data.getStringExtra("port1");
           data.getStringExtra("password1");}

       else if (requestCode == 200) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName2");   
               TextView user2 = (TextView)findViewById(R.id.user2);  
               user2.setText(sp.getString("accountName2", ""));}}

       else if ((requestCode == 250)&&(resultCode == RESULT_OK)) {
           data.getStringExtra("accountName2");  
           data.getStringExtra("wanIp2");
           data.getStringExtra("localIp2");
           data.getStringExtra("port2");
           data.getStringExtra("password2");}

       else if (requestCode == 300) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName3");   
               TextView user3 = (TextView)findViewById(R.id.user3);  
               user3.setText(sp.getString("accountName3", ""));
           }
       }
       else if (requestCode == 400) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName4");   
               TextView user4 = (TextView)findViewById(R.id.user4);  
               user4.setText(sp.getString("accountName4", ""));
           }
       }
       else if (requestCode == 500) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName5");   
               TextView user5  = (TextView)findViewById(R.id.user5);  
               user5.setText(sp.getString("accountName5", ""));
           }
       }
       else if (requestCode == 600) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName6");   
               TextView user6 = (TextView)findViewById(R.id.user6);  
               user6.setText(sp.getString("accountName6", ""));
           }
       }
       else if (requestCode == 700) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName7");   
               TextView user7 = (TextView)findViewById(R.id.user7);  
               user7.setText(sp.getString("accountName7", ""));
           }
       }
       else if (requestCode == 800) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName8");   
               TextView user8 = (TextView)findViewById(R.id.user8);  
               user8.setText(sp.getString("accountName8", ""));
           }
       }
       else if (requestCode == 900) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName9");   
               TextView user9 = (TextView)findViewById(R.id.user9);  
               user9.setText(sp.getString("accountName9", ""));
           }
       }
       else if (requestCode == 1000) {
           if (resultCode == RESULT_OK) {      
               data.getStringExtra("accountName10");   
               TextView user10 = (TextView)findViewById(R.id.user10);  
               user10.setText(sp.getString("accountName10", ""));
           }
       }
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        TextView user1  = (TextView)findViewById(R.id.user1);
        user1.setText(sp.getString("accountName1", ""));
        TextView user2  = (TextView)findViewById(R.id.user2);
        user2.setText(sp.getString("accountName2", ""));
        TextView user3  = (TextView)findViewById(R.id.user3);
        user3.setText(sp.getString("accountName3", ""));
        TextView user4  = (TextView)findViewById(R.id.user4);
        user4.setText(sp.getString("accountName4", ""));
        TextView user5  = (TextView)findViewById(R.id.user5);
        user5.setText(sp.getString("accountName5", ""));
        TextView user6  = (TextView)findViewById(R.id.user6);
        user6.setText(sp.getString("accountName6", ""));
        TextView user7  = (TextView)findViewById(R.id.user7);
        user7.setText(sp.getString("accountName7", ""));
        TextView user8  = (TextView)findViewById(R.id.user8);
        user8.setText(sp.getString("accountName8", ""));
        TextView user9  = (TextView)findViewById(R.id.user9);
        user9.setText(sp.getString("accountName9", ""));
        TextView user10 = (TextView)findViewById(R.id.user10);
        user10.setText(sp.getString("accountName10", ""));
    }
}

下面是从B中的row-click开始D的代码片段,因为代码很长,所以我尽量说清楚~

tableRow1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                Intent Intent1 = new Intent(AccountSetup.this, BluGuardProto2Activity.class);
                Intent1.putExtra("activity", "activity1");
                startActivityForResult(Intent1, 150);}});
4

2 回答 2

1

我不确定我是否完全理解这个问题,但无论如何我都会尝试一下。因此,您想知道活动 B 中用于启动活动 D 的两个不同选项中的哪一个?当它使用Intent.putExtra()启动时,你不能简单地将它传递给 Activity D吗?

就像是:

public class ActivityB extends Activity {

    private void startActivityDFromFirstChoice() {
        Intent i = new Intent(this, ActivityD.class);
        i.putExtra("CHOICE", "first");
        startActivityForResult(i, 1234);
    }

    private void startActivityDFromSecondChoice() {
        Intent i = new Intent(this, ActivityD.class);
        i.putExtra("CHOICE", "second");
        startActivityForResult(i, 1234);
    }

}


public class ActivityD extends Activity {

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

        String choice = getIntent().getStringExtra("CHOICE");
        if(choice != null && choice.equals("first")) {
            // activity was launched from first choice in B
        }
        else if(choice != null && choice.equals("second")) {
            // activity was launched from second choice in B
        }
    }
}

现在,这个例子远非完美(传递整数或类似的字符串可能更好),但它说明了我的意思。

似乎您正在尝试做与我的示例类似的事情,但我注意到两件事:

  • 我似乎无法在活动 D 中找到一个地方,您实际上可以从活动 B 中获得额外的字符串。您必须在活动 D 的某处添加 getIntent().getStringExtra("activity")。
  • 在一个地方你做 Intent1.putExtra("activity1", "activity1"); 而在另一个 Intent2.putExtra("activity", "activity2"); 在这两种情况下不应该是“活动”作为附加键吗?
于 2013-05-14T06:47:48.887 回答
0

您可以在活动中使用 getCallingActivity 来获取调用活动。http://developer.android.com/reference/android/app/Activity.html#getCallingActivity()

于 2013-05-14T09:10:41.643 回答