确定哪个StartActivityForResult正在调用当前活动

时间:2013-05-14 06:36:58

标签: android

我的情况如下所述。

活动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中的行中单击启动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);}});

2 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解这个问题,但无论如何我都会尝试一下。因此,您想知道活动B中用于启动活动D的两个不同选项中的哪一个?你是否可以在使用Intent.putExtra()

启动时将其传递给活动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
        }
    }
}

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

看起来你正在尝试做一些类似于我的例子,但我注意到两件事:

  • 我似乎无法在Activity D中找到你实际获得从Activity B传递的extras字符串的地方。你必须在Activity D中的某处添加getIntent()。getStringExtra(“activity”)。
  • 在一个地方你做Intent1.putExtra(“activity1”,“activity1”);而在另一个Intent2.putExtra(“活动”,“活动2”);在这两种情况下,它不应该是“活动”作为额外的关键吗?

答案 1 :(得分:0)

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