-1

次に、私のアプリには、SplashActivity -> メニュー ---> Q_001 ---> Q_002 ... (...) ..... Q_finish の順に 103 のアクティビティがあります。各アクティビティには、アプリを終了するボタン (ホーム ボタンなど) と、メニューに戻るボタンがあります。ユーザーがアプリを強制終了し、別のときにアプリを開くと、使用していたアクティビティに戻ります。

私は、SpalshActivity を変更しようとしなければならないことを知っています。あなたが行っていた最後のアクティビティです。彼のワークを継続するためのアクティビティを実行していない場合、これは SplashActivity のコードです。

package org.somename;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle TravisIsAwesome) {
        super.onCreate(TravisIsAwesome);
        setContentView(R.layout.splash);



        Thread logoTimer = new Thread (){


            @Override
            public void run(){
                try{
                    sleep(500);
                    Intent menuIntent = new Intent("org.somename.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();

                }
            }
    };

    logoTimer.start();


    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

活動メニューを保存することは気にしないと言い続けますが、「Q_001」から始まるすべての活動と活動「Q_finish」を終了するすべての活動。新しいこのメソッドを使用しようとしました: 前の質問で提案し、このコードを実装しました:

package org.somename;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle TravisIsAwesome) {
        super.onCreate(TravisIsAwesome);
        setContentView(R.layout.splash);

         int last_activity = getLastActivityIdFromSharedPreferences();
            if (last_activity == 1)
            {
                this.startActivity(new Intent(this, Q_001.class));
                finish();
            }

            int last_activity2 = getLastActivityIdFromSharedPreferences2();
            if (last_activity2 == 2)
            {
                this.startActivity(new Intent(this, Q_002.class));
                finish();
            }

        Thread logoTimer = new Thread (){


            @Override
            public void run(){
                try{
                    sleep(500);
                    Intent menuIntent = new Intent("org.somename.MENU");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();

                }
            }
    };

    logoTimer.start();


    }






    private int getLastActivityIdFromSharedPreferences2() {
        // TODO Auto-generated method stub
        return 2;
    }






    private int getLastActivityIdFromSharedPreferences() {
        // TODO Auto-generated method stub
        return 1;
    }






    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ただし、SprahActivity の一部ではなく、実行された Activity には戻りません。空白ページの SprahActivity を使用して、メニューに直接移動します

少し早いですがお礼を

4

2 に答える 2

0

I agree with @2Dee.

First thoughts were that your are not really learning the current Android development, I think. I suppose most people would go for one activity for splash (if you really need one) and a second activity for holding the 100 buttons. This 100 buttons would be hold inside a viewpager. It is a bit more complex to understand at first but it is the way to go, in my opinion. The view pager will recycle, is efficient and a powerful tool to leverage.

But maybe you are doing a school exercise ?

Edit: I see 2 ways, probably someone can find better.

  1. You can save in memory where the user is. When the user click to exit, just save in a SharedPreferences the current question and retrieve it at start : sharedPreferences
  2. You can make that clicking exit button jsut call onBackPressed() and maybe override onBackPressed. If you do that the activity will be send to background and the user to Home. If he goes back to it, it will restart on that last activity. You must still save it in case the user kill the app or Android kills the app for ressource management.

Edit 2 :

In Splash Activity, you retrieve an integer. in the main activity (the one holding the question and the buttons) you write the question number.

That gave something like that :

splash activity :

 @Override
protected void onCreate(Bundle state){
   super.onCreate(state);
   . . .

   // Restore preferences
   SharedPreferences settings = getSharedPreferences(LAST_QUESTION, 0);
   int number = settings.getInt("last_question", 1); // default value is to start again
   goToQuestionNumber(number); // you better call this in onResume(). 
}

In the exiting method launched by the onClik()

 SharedPreferences settings = getSharedPreferences(LAST_QUESTION, 0);
  SharedPreferences.Editor editor = settings.edit();
  editor.putInt("last_question", currentQuestionNumberThatYouShouldKnow);
于 2013-11-08T13:50:44.747 に答える
0

わかりました、私が思ったように、アプリのアーキテクチャを再考する必要があると思います:)

1 つのアクティビティと 1 つのフラグメントを作成します。アクティビティは、ビュー内のフラグメントを切り替えて作成する責任があります。また、Web ビューに必要な URL への参照も保持します。

これは私が行う方法です(ただし、コードの完全なリファクタリングが必要になる場合があります-考えてみれば、これは悪い練習ではありません):

活動中:

// declare your urls in the res/values/strings.xml and use them here.
private int[] webViewsUrls = {R.string.url1, R.string.url2, ...}; 

アクティビティの onCreate で:

SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
int activityIndex = prefs.getInt("lastActivityIndex", 0);
QuestionFragment fragment = new QuestionFragment();
Bundle bundle = new Bundle();
bundle.putInt("INDEX", activityIndex);
bundle.putString("WEBVIEW_URL", webViewsUrls[activityIndex]);
fragment.setArguments(bundle);
// Show fragment

アクティビティ レイアウト内の ViewGroup に新しい Fragment を追加するには (ViewPager を使用せずにボタンを使用してビュー内の質問を変更する場合):

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// create you fragment using the method I give above
fragmentTransaction.add(R.id.fragment_container_in_activity_layout, fragment, STRING_FRAGMENT_TAG);
fragmentTransaction.commit();

すでに表示されているフラグメントを置き換えるには、add の代わりにreplaceメソッドを使用します。

ボタンをクリックするか、ViewPager でビュー内の Fragment を変更し、Fragment を開始するときに SharedPreferences に activityIndex の新しい値を保存することを忘れないでください。

@Poutrathor の提案は非常に優れていると思います。ViewPager を使用する場合は、昨日私が行ったこの回答を参照してください。これは、100 以上のアクティビティを使用するよりも要件に適していると思います。

于 2013-11-08T14:37:19.647 に答える